JDBC
1.JDBC本质:官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口。各个数据库厂商去实现这套接口,
提供数据库驱动jar包。我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类。
2. 快速入门:
* 步骤:
1).导入驱动jar包,如: mysql-connector-java-5.1.37-bin.jar
2).注册驱动
3).获取数据库连接对象 Connection
4).定义sql
5).获取执行sql语句的对象 Statement
6).执行sql,接受返回结果
7).处理结果
8).释放资源
3.详细解读:
1. DriverManager:驱动管理对象
* 功能:
1. 注册驱动:告诉程序该使用哪一个数据库驱动jar
static void registerDriver(Driver driver) :注册与给定的驱动程序 DriverManager 。
写代码使用: Class.forName("com.mysql.jdbc.Driver");
通过查看源码发现:在com.mysql.jdbc.Driver类中存在静态代码块
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
注意:mysql5之后的驱动jar包可以省略注册驱动的步骤。
2. 获取数据库连接:
* 方法:static Connection getConnection(String url, String user, String password)
* 参数:
* url:指定连接的路径
* 语法:jdbc:mysql://ip地址(域名):端口号/数据库名称
* 例子:jdbc:mysql://localhost:3306/db3
* 细节:如果连接的是本机mysql服务器,并且mysql服务默认端口是3306,则url可以简写为:jdbc:mysql:///数据库名称
* user:用户名
* password:密码
3. Connection:数据库连接对象
1. 功能:
1. 获取执行sql 的对象
* Statement createStatement()
* PreparedStatement prepareStatement(String sql)
2. 管理事务:
* 开启事务:setAutoCommit(boolean autoCommit) :调用该方法设置参数为false,即开启事务
* 提交事务:commit()
* 回滚事务:rollback()
4. Statement:执行sql的对象
1. 执行sql
1. boolean execute(String sql) :可以执行任意的sql 了解
2. int executeUpdate(String sql) :执行DML(insert、update、delete)语句、DDL(create,alter、drop)语句
* 返回值:影响的行数,可以通过这个影响的行数判断DML语句是否执行成功 返回值>0的则执行成功,反之,则失败。
3. ResultSet executeQuery(String sql) :执行DQL(select)语句
案例:向表中插入数据:
代码:
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.Statement; 4 5 public class Demo1 { 6 public static void main(String[] args) throws Exception { 7 //1. 注册驱动 8 Class.forName("com.mysql.jdbc.Driver"); 9 //2.获取数据库连接对象 10 Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/JdbcDB", "root", "123456"); 11 //3.定义sql语句 12 String sql = "insert into account(`uname`,`password`,`email`) values('马化腾','789','789@1')"; 13 //4.获取执行sql的对象 Statement 14 Statement statement = connection.createStatement(); 15 //5.执行sql 16 int count = statement.executeUpdate(sql); 17 if (count > 0) { 18 System.out.println("成功"); 19 } else { 20 System.out.println("失败"); 21 } 22 //释放资源 23 connection.close(); 24 statement.close(); 25 } 26 }
案例:在表中修改数据:
代码:
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.SQLException; 4 import java.sql.Statement; 5 6 public class Demo2 { 7 public static void main(String[] args) { 8 Connection connection = null; 9 Statement statement = null; 10 try { 11 //1. 注册驱动 12 Class.forName("com.mysql.jdbc.Driver"); 13 //2.获取Connection对象 14 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/JdbcDB", "root", "123456"); 15 //3. 定义sql 16 String sql = "update account set uname = '王余阳' where id = 2"; 17 //4.获取执行sql的对象 Statement 18 statement = connection.createStatement(); 19 //5.执行sql 20 int count = statement.executeUpdate(sql); 21 if (count > 0) { 22 System.out.println("成功"); 23 } else { 24 System.out.println("失败"); 25 } 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } 29 try { 30 connection.close(); 31 } catch (SQLException e) { 32 e.printStackTrace(); 33 } 34 try { 35 statement.close(); 36 } catch (SQLException e) { 37 e.printStackTrace(); 38 } 39 } 40 }
ResultSet:结果集对象,封装查询结果
* boolean next(): 游标向下移动一行,判断当前行是否是最后一行末尾(是否有数据),如果是,则返回false,如果不是则返回true
* getXxx(参数):获取数据
* Xxx:代表数据类型 如: int getInt() , String getString()
* 参数:
1. int:代表列的编号,从1开始 如: getString(1)
2. String:代表列名称。 如: getDouble("balance")
* 注意:
* 使用步骤:
1. 游标向下移动一行
2. 判断是否有数据
3. 获取数据
案例:查询数据
代码:
1 import java.sql.*; 2 3 public class Demo3 { 4 public static void main(String[] args) { 5 Connection connection = null; 6 Statement statement = null; 7 ResultSet resultSet = null; 8 try { 9 Class.forName("com.mysql.jdbc.Driver"); 10 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/JdbcDB", "root", "123456"); 11 String sql = "select * from account"; 12 statement = connection.createStatement(); 13 resultSet = statement.executeQuery(sql); 14 while (resultSet.next()) { 15 int id = resultSet.getInt("id"); 16 String uname = resultSet.getString("uname"); 17 String password = resultSet.getString(3); 18 String email = resultSet.getString(4); 19 System.out.println("id: " + id + "uname:" + uname + "password: " + password + "email: " + email); 20 } 21 } catch (Exception e) { 22 e.printStackTrace(); 23 } 24 try { 25 connection.close(); 26 } catch (SQLException e) { 27 e.printStackTrace(); 28 } 29 try { 30 statement.close(); 31 } catch (SQLException e) { 32 e.printStackTrace(); 33 } 34 try { 35 resultSet.close(); 36 } catch (SQLException e) { 37 e.printStackTrace(); 38 } 39 } 40 }
案例:创建账户类,在对数据库的表进行查询
代码:
1 public class Account { 2 private int id; 3 private String uname; 4 private String password; 5 private String email; 6 7 public int getId() { 8 return id; 9 } 10 11 public void setId(int id) { 12 this.id = id; 13 } 14 15 public String getUname() { 16 return uname; 17 } 18 19 public void setUname(String uname) { 20 this.uname = uname; 21 } 22 23 public String getPassword() { 24 return password; 25 } 26 27 public void setPassword(String password) { 28 this.password = password; 29 } 30 31 public String getEmail() { 32 return email; 33 } 34 35 public void setEmail(String email) { 36 this.email = email; 37 } 38 39 @Override 40 public String toString() { 41 return "Account{" + 42 "id=" + id + 43 ", uname='" + uname + '\'' + 44 ", password='" + password + '\'' + 45 ", email='" + email + '\'' + 46 '}'; 47 } 48 }
1 import java.sql.*; 2 import java.util.ArrayList; 3 import java.util.List; 4 5 public class Demo4 { 6 public static void main(String[] args) { 7 List<Account> list = obtain(); 8 System.out.println(list); 9 } 10 11 public static List<Account> obtain() { 12 Connection connection = null; 13 Statement statement = null; 14 ResultSet resultSet = null; 15 List<Account> list = new ArrayList<Account>(); 16 try { 17 Class.forName("com.mysql.jdbc.Driver"); 18 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/JdbcDB", "root", "123456"); 19 String sql = "select * from account"; 20 statement = connection.createStatement(); 21 resultSet = statement.executeQuery(sql); 22 Account account = null; 23 24 while (resultSet.next()) { 25 int id = resultSet.getInt("id"); 26 String uname = resultSet.getString("uname"); 27 String password = resultSet.getString("password"); 28 String email = resultSet.getString("email"); 29 account = new Account(); 30 account.setId(id); 31 account.setUname(uname); 32 account.setPassword(password); 33 account.setEmail(email); 34 list.add(account); 35 } 36 } catch (Exception e) { 37 e.printStackTrace(); 38 } 39 if (connection != null) { 40 try { 41 connection.close(); 42 } catch (SQLException e) { 43 e.printStackTrace(); 44 } 45 } 46 if (statement != null) { 47 try { 48 statement.close(); 49 } catch (SQLException e) { 50 e.printStackTrace(); 51 } 52 } 53 if (resultSet != null) { 54 try { 55 resultSet.close(); 56 } catch (SQLException e) { 57 e.printStackTrace(); 58 } 59 } 60 return list; 61 } 62 }

浙公网安备 33010602011771号