赞
踩
今天学习了使用JDBC对数据库进行操作,现总结如下
一、七个步骤
1.加载数据库驱动到JVM中
Class.forName("com.mysql.jdbc.Driver");//forName()方法可将指定字符串的类加载到JVM中
2.建立数据库连接
首先要构建数据库连接的URL以及数据库的用户名和密码。‘
String url = "jdbc:mysql://localhost:3306/std";//3306为端口号,std用自己数据库名字代替
String username = "root";
String password = "123456";
使用DriverMager对象的 getConnection()方法创建Connection对象
Connection connection1 = DriverManager.getConnection(url, username, password);
3.创建数据库作对象
在JDBC的API中封装了Statement的扩展PreparedStaement,应用于其中的SQL语句,可用占位符"?'来代替参数。
PreparedStatement statement = null;
4.定义SQL语句
定义增删改查等SQL语句用于数据库操作
String sq3 = "delete from user where id=4";
5.执行数据库操作
statement = connection1.prepareStatement(sq3);
6获取操作结果集
ResultSet接口封装了数据查询的结果集,针对Java中数据提供了一套get***()方法,来获取每一行的数据。注意对于delete,update,insert 操作没有Resultset对象返回。select操作会返回Resultset对象。
resultSet=statement.executeQuery();
7关闭数据库连接
在对数据库的操作结束后一定要及时关闭数据库资源,关闭时,对Resultset,Connection,PrepareStaement对象都要关闭。
if(resultSet!=null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if(statement!=null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if(connection!=null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); }
增删改查操作完整代码如下:
public class DBop { //连接数据库 public static Connection getconnect() { Connection connection = null; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/std?useSSL=true&" + "characterEncoding=utf-8&user=root&password=123456"); System.out.println("成功"); return connection; } catch (Exception e) { e.printStackTrace(); return null; } } //查询数据库 public static void Selectdb() { String sql = "select* from user"; Connection connection = getconnect(); PreparedStatement statement=null; try { statement = connection.prepareStatement(sql); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()) { System.out.println(resultSet.getString(1)); System.out.println(resultSet.getString(2)); System.out.println(resultSet.getString(3)); } } catch (Exception e){ e.printStackTrace(); } } //增加数据 public static void insertdb() { String sq2 = "insert into user values(?,?,?)"; Connection connection = getconnect(); PreparedStatement statement = null; try { statement = connection.prepareStatement(sq2); statement.setString(1, "6p"); statement.setString(2, "183"); statement.setString(3, "2"); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } //删除数据 public static void deletedb() { PreparedStatement statement=null; String sq3 = "delete from user where id=2"; Connection connection = getconnect(); try { statement = connection.prepareStatement(sq3); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } //更新数据 public static void updatedb() { String sq4 = "update score set grade=200 where id=6"; Connection connection = getconnect(); PreparedStatement statement; try { statement = connection.prepareStatement(sq4); statement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } //关闭数据库} public static void closeconnection(Connection connection, PreparedStatement statement, ResultSet resultSet) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { e.printStackTrace(); } } if (statement != null) { try { statement.close(); } catch (SQLException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (SQLException e) { e.printStackTrace(); } System.out.println("成功关闭"); } }
总结:JDBC操作要求我们牢记七个步骤并,值得注意的是一些数据库异常的处理,定义Connection,PreparedStamanet,等对象时最好在try语句之前,以免出现变量未定义的提示,此外,根据封装的思想,可将连接,关闭等数据库操作封装为函数,方便其他类调用。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。