JDBC(一)
JDBC 是使用JAVA语言操作关系型数据库的API 步骤如下:
- 连接数据库.
- 导入驱动jar包
2. 注册驱动
1 2 3 4 5 | Class.forName(driverClass) //加载MySql驱动 Class.forName( "com.mysql.jdbc.Driver" ) //加载Oracle驱动 Class.forName( "oracle.jdbc.driver.OracleDriver" ) |
3. 获取数据库连接
1 2 | private static Connection conn = null ;<br><br>conn = DriverManager.getConnection( "url" , "username" , "password" ); //url : jdbc:mysql://127.0.0.1:3306/数据库名称 //username 和 password 分别是自己数据库账号和密码<br>//也可以用参数 |
4. 定义sql 语句
1 | String sql = "" ; |
5. 获取执行sql对象 Statement.
1 | Statement stmt = conn.createStatement();<br> int i = stmt.executeUpdate(sql); //数据库中受影响的行数,返回整数 |
6. PreparedStatement //预编译SQL,减少sql执行
1 2 3 | PreparedStatement ptmt = conn.prepareStatement(sql); //传参 ptmt.setInt( 1 , id); //前面数字是sql语句中的? |
7. 获取查询结果的结果集对象: ResultSet //查询时用
1 | ResultSet rs = stmt.executeQuery(); //执行<br> while(rs.next()){ //遍历查询数据,每次光标往下移动一行<br> int id = re.getInt("id");<br> String name = rs.getString("name");<br>} |
6. 释放资源
1 2 | rs.close();<br>stmt.close(); conn.close(); |
- 为数据库传递指令
- 处理数据库响应并返回结果
完整实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 | 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" ; private static Connection conn = null ; static { try { //1.加载驱动程序 Class.forName( "com.mysql.jdbc.Driver" ); //2. 获得数据库连接 conn = DriverManager.getConnection(URL, USER, PASSWORD); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public static Connection getConnection(){ return conn; } } //模型 package liulx.model; import java.util.Date; public class Goddess { private Integer id; private String user_name; private Integer sex; private Integer age; private Date birthday; //注意用的是java.util.Date private String email; private String mobile; private String create_user; private String update_user; private Date create_date; private Date update_date; private Integer isDel; //getter setter方法。。。 } //---------dao层-------------- package liulx.dao; import liulx.db.DbUtil; import liulx.model.Goddess; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class GoddessDao { //增加 public void addGoddess(Goddess g) throws SQLException { //获取连接 Connection conn = DbUtil.getConnection(); //sql String sql = "INSERT INTO imooc_goddess(user_name, sex, age, birthday, email, mobile," + "create_user, create_date, update_user, update_date, isdel)" + "values(" + "?,?,?,?,?,?,?,CURRENT_DATE(),?,CURRENT_DATE(),?)" ; //预编译 PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL,减少sql执行 //传参 ptmt.setString( 1 , g.getUser_name()); ptmt.setInt( 2 , g.getSex()); ptmt.setInt( 3 , g.getAge()); ptmt.setDate( 4 , new Date(g.getBirthday().getTime())); ptmt.setString( 5 , g.getEmail()); ptmt.setString( 6 , g.getMobile()); ptmt.setString( 7 , g.getCreate_user()); ptmt.setString( 8 , g.getUpdate_user()); ptmt.setInt( 9 , g.getIsDel()); //执行 ptmt.execute(); } public void updateGoddess(){ //获取连接 Connection conn = DbUtil.getConnection(); //sql, 每行加空格 String sql = "UPDATE imooc_goddess" + " set user_name=?, sex=?, age=?, birthday=?, email=?, mobile=?," + " update_user=?, update_date=CURRENT_DATE(), isdel=? " + " where id=?" ; //预编译 PreparedStatement ptmt = conn.prepareStatement(sql); //预编译SQL,减少sql执行 //传参 ptmt.setString( 1 , g.getUser_name()); ptmt.setInt( 2 , g.getSex()); ptmt.setInt( 3 , g.getAge()); ptmt.setDate( 4 , new Date(g.getBirthday().getTime())); ptmt.setString( 5 , g.getEmail()); ptmt.setString( 6 , g.getMobile()); ptmt.setString( 7 , g.getUpdate_user()); ptmt.setInt( 8 , g.getIsDel()); ptmt.setInt( 9 , g.getId()); //执行 ptmt.execute(); } public void delGoddess(){ //获取连接 Connection conn = DbUtil.getConnection(); //sql, 每行加空格 String sql = "delete from imooc_goddess where id=?" ; //预编译SQL,减少sql执行 PreparedStatement ptmt = conn.prepareStatement(sql); //传参 ptmt.setInt( 1 , id); //执行 ptmt.execute(); } public List<Goddess> query() throws SQLException { Connection conn = DbUtil.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery( "SELECT user_name, age FROM imooc_goddess" ); List<Goddess> gs = new ArrayList<Goddess>(); Goddess g = null ; while (rs.next()){ g = new Goddess(); g.setUser_name(rs.getString( "user_name" )); g.setAge(rs.getInt( "age" )); gs.add(g); } return gs; } public Goddess get(){ Goddess g = null ; //获取连接 Connection conn = DbUtil.getConnection(); //sql, 每行加空格 String sql = "select * from imooc_goddess where id=?" ; //预编译SQL,减少sql执行 PreparedStatement ptmt = conn.prepareStatement(sql); //传参 ptmt.setInt( 1 , id); //执行 ResultSet rs = ptmt.executeQuery(); while (rs.next()){ g = new Goddess(); g.setId(rs.getInt( "id" )); g.setUser_name(rs.getString( "user_name" )); g.setAge(rs.getInt( "age" )); g.setSex(rs.getInt( "sex" )); g.setBirthday(rs.getDate( "birthday" )); g.setEmail(rs.getString( "email" )); g.setMobile(rs.getString( "mobile" )); g.setCreate_date(rs.getDate( "create_date" )); g.setCreate_user(rs.getString( "create_user" )); g.setUpdate_date(rs.getDate( "update_date" )); g.setUpdate_user(rs.getString( "update_user" )); g.setIsDel(rs.getInt( "isdel" )); } return g; }} |
部分知识点来自:https://www.runoob.com/w3cnote/jdbc-use-guide.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· DeepSeek在M芯片Mac上本地化部署
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能