jdbc 连接数据库学习
String url="jdbc:mysql://127.0.0.1:3306/pm?useUnicode=true&characterEncoding=utf8mb4&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8"; String user="root"; String password="root"; try{ //加载驱动 Class.forName("com.mysql.jdbc.Driver"); //创建连接 Connection conn= DriverManager.getConnection(url,user,password); //预编译执行器 PreparedStatement ps= conn.prepareStatement("select * from dept_table"); //获取结果集 ResultSet rs= ps.executeQuery();//查询 // ps.executeUpdate();增删改 while(rs.next()){ int deptnum= rs.getInt("department_number"); System.out.println(deptnum); } // rs.close(); // ps.close(); // conn.close(); }catch (Exception ex){ ex.printStackTrace(); }finally { Statement statement = connection.createStatement(); }