oracle
//1加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); //2.创建连接 String url="jdbc:oracle:thin:@dulocalhost:1521:orcl"; //orcl为数据库的SID String user="test"; String password="test"; Connection conn= DriverManager.getConnection(url,user,password); //3.创建执行语句 PreparedStatement ps = con.prepareStatement("select * from user") PreparedStatement ps = ps.executeQuery(); //4.遍历结果集 while (rs.next()) { System.out.println(rs.getString("id") + " " + rs.getString("name"); } //5.关闭连接 ps.close(); conn.close();
mysql
//1加载驱动 Class.forName("com.mysql.jdbc.Driver"); //2.创建连接 String url="jdbc:mysql://localhost:3306/jdbc"; String user="root"; String password="123456"; Connection conn= DriverManager.getConnection(url,user,password); //3.创建执行语句 PreparedStatement ps = con.prepareStatement("select * from user") PreparedStatement ps = ps.executeQuery(); //4.遍历结果集 while (rs.next()) { System.out.println(rs.getString("id") + " " + rs.getString("name"); } //5.关闭连接 ps.close(); conn.close();