JDBC连接数据库
public class JdbcDemo1 { public static void main(String[] args) throws SQLException { //1.注册驱动
//Class.forName("com.mysql.jdbc.Driver")
//上面的方式只会没有jar包会报运行时异常,下面的写法会在编译时报异常 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); //2.获取连接 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/spring", "root", "123456"); //3.获取操作数据库的预处理对象 PreparedStatement pstm = conn.prepareStatement("select * from account"); //4.执行sql ResultSet rs = pstm.executeQuery(); //5.处理结果集 while(rs.next()){ System.out.println(rs.getString("name")); } //6.关闭资源 rs.close(); pstm.close(); conn.close(); } }