加载驱动程序:
Class.forName(driverClass)//加载MySql驱动Class.forName("com.mysql.jdbc.Driver")//加载Oracle驱动Class.forName("oracle.jdbc.driver.OracleDriver")
获得数据库连接:
DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/imooc", "root", "root");
创建Statement\PreparedStatement对象:
conn.createStatement();conn.prepareStatement(sql);完整实例import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;public class DbUtil { public static final String URL = "jdbc:mysql://localhost:3306/imooc"; public static final String USER = "liulx"; public static final String PASSWORD = "123456"; public static void main(String[] args) throws Exception { //1.加载驱动程序 Class.forName("com.mysql.jdbc.Driver"); //2. 获得数据库连接 Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); //3.操作数据库,实现增删改查 Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT user_name, age FROM imooc_goddess"); //如果有数据,rs.next()返回true while(rs.next()){ System.out.println(rs.getString("user_name")+" 年龄:"+rs.getInt("age")); } }}
Class.forName(driverClass)//加载MySql驱动Class.forName("com.mysql.jdbc.Driver")//加载Oracle驱动Class.forName("oracle.jdbc.driver.OracleDriver")
获得数据库连接:
DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/imooc", "root", "root");
创建Statement\PreparedStatement对象:
conn.createStatement();conn.prepareStatement(sql);完整实例import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;public class DbUtil { public static final String URL = "jdbc:mysql://localhost:3306/imooc"; public static final String USER = "liulx"; public static final String PASSWORD = "123456"; public static void main(String[] args) throws Exception { //1.加载驱动程序 Class.forName("com.mysql.jdbc.Driver"); //2. 获得数据库连接 Connection conn = DriverManager.getConnection(URL, USER, PASSWORD); //3.操作数据库,实现增删改查 Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT user_name, age FROM imooc_goddess"); //如果有数据,rs.next()返回true while(rs.next()){ System.out.println(rs.getString("user_name")+" 年龄:"+rs.getInt("age")); } }}