java连接数据库步骤 import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class JdbcTest {public static void main(String[] args) throws SQLExce
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcTest { public static void main(String[] args) throws SQLException, ClassNotFoundException { 1.注册驱动 // DriverManager.registerDriver(new com.mysql.jdbc.Driver()); // System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver"); //推荐 2.建立连接 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myjdbc","root",""); //最后是数据库的名字 和登录数据库的用户名 密码 3创建语句 Statement st = conn.createStatement(); 4.执行语句 ResultSet rs = st.executeQuery("select * from class"); 5.处理结果 while(rs.next()){ System.out.println(rs.getObject(1)+"\t"+rs.getObject(2)+"\t"+rs.getObject(3)); } 6.释放资源 rs.close(); st.close(); conn.close(); } }