哪个网站做服装定制好,门户网站系统建设项目投标书,德阳网站建设公司,wordpress 备份页面目录
一、基于PreparedStatement实现CRUD
1.查询单行单列
2.查询单行多列
3.查询多行多列
4.新增
5.修改
6.删除
7.总结
二、常见问题
1.资源的管理
2.SQL语句问题
3.SQL语句未设置参数问题
4.用户名或密码错误问题
5.通信异常
总结 一、基于PreparedStatement实…目录
一、基于PreparedStatement实现CRUD
1.查询单行单列
2.查询单行多列
3.查询多行多列
4.新增
5.修改
6.删除
7.总结
二、常见问题
1.资源的管理
2.SQL语句问题
3.SQL语句未设置参数问题
4.用户名或密码错误问题
5.通信异常
总结 一、基于PreparedStatement实现CRUD
1.查询单行单列
package com.myblog;import java.sql.*;
import org.junit.Test;public class JdbcExample {Testpublic void testQuerySingleRowAndCol() throws Exception {//1.注册驱动可以省略//2.获取连接Connection connection DriverManager.getConnection(jdbc:mysql:///myblog_db, root, your_password); // 请替换为你的数据库密码//3.预编译SQL语句得到PreparedStatement对象PreparedStatement preparedStatement connection.prepareStatement(SELECT COUNT(*) as count FROM employee);//4.执行SQL语句获取结果ResultSet resultSet preparedStatement.executeQuery();//5.处理结果// 如果自己明确一定只有一个结果那么resultSet最少要做一next的判断才能拿到我们要的列的结果if (resultSet.next()) {int count resultSet.getInt(count);System.out.println(count);}//6.释放资源resultSet.close();preparedStatement.close();connection.close();}
}2.查询单行多列
package com.myblog;import java.sql.*;
import org.junit.Test;public class JdbcExample {Testpublic void testQueryRowAndCol() throws Exception {//1.注册驱动略//2.获取连接对象Connection connection DriverManager.getConnection(jdbc:mysql:///myblog_db, root, your_password); // 请替换为你的数据库密码//3.预编译SQL语句得到PreparedStatement对象PreparedStatement preparedStatement connection.prepareStatement(SELECT id, name, salary, age FROM employee WHERE id ?);//4.执行SQL语句获取结果//有占位符要赋值preparedStatement.setInt(1, 5);ResultSet resultSet preparedStatement.executeQuery();//5.处理结果while (resultSet.next()) {int id resultSet.getInt(id);String name resultSet.getString(name);double salary resultSet.getDouble(salary);int age resultSet.getInt(age);System.out.println(id \t name \t salary \t age);}//6.资源释放resultSet.close();preparedStatement.close();connection.close();}
}3.查询多行多列
package com.myblog;import java.sql.*;
import org.junit.Test;public class JdbcExample {Testpublic void testQueryMoreRow() throws Exception {// 获取连接对象Connection connection DriverManager.getConnection(jdbc:mysql:///myblog_db, root, your_password); // 请替换为你的数据库密码// 预编译SQL语句得到PreparedStatement对象PreparedStatement preparedStatement connection.prepareStatement(SELECT id, name, salary, age FROM employee WHERE age ?);// 为占位符赋值并接受结果preparedStatement.setInt(1, 25);ResultSet resultSet preparedStatement.executeQuery();// 处理结果while (resultSet.next()) {int id resultSet.getInt(id);String name resultSet.getString(name);double salary resultSet.getDouble(salary);int age resultSet.getInt(age);System.out.println(id \t name \t salary \t age);}// 释放资源resultSet.close();preparedStatement.close();connection.close();}
}4.新增
package com.myblog;import java.sql.*;
import org.junit.Test;public class JdbcExample {Testpublic void testInsert() throws Exception {// 获取连接对象Connection connection DriverManager.getConnection(jdbc:mysql:///myblog_db, root, your_password); // 请替换为你的数据库密码// 预编译SQL语句得到PreparedStatement对象PreparedStatement preparedStatement connection.prepareStatement(INSERT INTO employee(name, salary, age) VALUES(?,?,?));// 为占位符赋值preparedStatement.setString(1, rose);preparedStatement.setDouble(2, 345.67);preparedStatement.setInt(3, 28);// 执行插入操作int result preparedStatement.executeUpdate();// 根据受影响行数判断操作是否成功if (result 0) {System.out.println(插入成功);} else {System.out.println(插入失败);}// 释放资源preparedStatement.close();connection.close();}
}5.修改
package com.myblog;import java.sql.*;
import org.junit.Test;public class JdbcExample {Testpublic void testUpdate() throws Exception {// 获取连接对象Connection connection DriverManager.getConnection(jdbc:mysql:///myblog_db, root, your_password); // 请替换为你的数据库密码// 预编译SQL语句得到PreparedStatement对象PreparedStatement preparedStatement connection.prepareStatement(UPDATE employee SET salary ? WHERE id ?);// 为占位符赋值preparedStatement.setDouble(1, 8888.88);preparedStatement.setInt(2, 6);// 执行更新操作int result preparedStatement.executeUpdate();// 根据受影响行数判断操作是否成功if (result 0) {System.out.println(更新成功);} else {System.out.println(更新失败...);}// 释放资源preparedStatement.close();connection.close();}
}6.删除
package com.myblog;import java.sql.*;
import org.junit.Test;public class JdbcExample {Testpublic void testDelete() throws Exception {// 获取连接对象Connection connection DriverManager.getConnection(jdbc:mysql:///myblog_db, root, your_password); // 请替换为你的数据库密码// 预编译SQL语句得到PreparedStatement对象PreparedStatement preparedStatement connection.prepareStatement(DELETE FROM employee WHERE id ?);// 为占位符赋值preparedStatement.setInt(1, 6);// 执行删除操作int result preparedStatement.executeUpdate();// 根据受影响行数判断操作是否成功if (result 0) {System.out.println(删除成功);} else {System.out.println(删除失败...);}// 释放资源preparedStatement.close();connection.close();}
}7.总结
注册驱动通常通过Class.forName()注册JDBC驱动这一步可以省略。获取连接通过DriverManager.getConnection()获取数据库连接。创建PreparedStatement对象编写SQL语句使用Connection.prepareStatement()创建PreparedStatement对象。设置参数通过setXXX方法为SQL语句中的占位符赋值。执行操作通过executeQuery()执行查询操作通过executeUpdate()执行插入、更新或删除操作。处理结果查询操作需要遍历ResultSet对象处理结果。插入、更新和删除操作通过受影响的行数判断操作是否成功。释放资源按顺序关闭ResultSet、PreparedStatement和Connection对象确保资源不泄露。 二、常见问题
1.资源的管理 在使用JDBC的相关资源时比如Connection、PreparedStatement、ResultSet使用完毕后要及时关闭这些资源以释放数据库服务器资源和避免内存泄漏是很重要的。
2.SQL语句问题
java.sql.SQLSyntaxErrorExceptionSQL语句错误异常一般有几种可能 SQL语句有错误检查SQL语句建议SQL语句在SQL工具中测试后再复制到Java程序中 连接数据库的URL中数据库名称编写错误也会报该异常
3.SQL语句未设置参数问题
java.sql.SQLExceptionNo value specified for parameter 1
在使用预编译SQL语句时如果有?占位符要为每一个占位符赋值否则报该错误
4.用户名或密码错误问题
java.sql.SQLException: Access denied for user root123localhost (using password: YES)
连接数据库时如果用户名或密码输入错误也会报SQLException容易混淆所以一定要看清楚异常后面的原因描述
5.通信异常
在连接数据库的URL中如果IP或端口写错了会报如下异常
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure 总结 本篇对JDBC技术的基础增删改查和常见错误进行了汇总便于理解和运用。部分内容源自网络如有侵权请联系作者删除谢谢 文章转载自: http://www.morning.rwzqn.cn.gov.cn.rwzqn.cn http://www.morning.c7498.cn.gov.cn.c7498.cn http://www.morning.ztfzm.cn.gov.cn.ztfzm.cn http://www.morning.mrbmc.cn.gov.cn.mrbmc.cn http://www.morning.yhywx.cn.gov.cn.yhywx.cn http://www.morning.prmyx.cn.gov.cn.prmyx.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.lnsnyc.com.gov.cn.lnsnyc.com http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.rftk.cn.gov.cn.rftk.cn http://www.morning.mhybs.cn.gov.cn.mhybs.cn http://www.morning.gjqgz.cn.gov.cn.gjqgz.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.snccl.cn.gov.cn.snccl.cn http://www.morning.wcgcm.cn.gov.cn.wcgcm.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.c7507.cn.gov.cn.c7507.cn http://www.morning.lfqtp.cn.gov.cn.lfqtp.cn http://www.morning.tgtsg.cn.gov.cn.tgtsg.cn http://www.morning.hfbtt.cn.gov.cn.hfbtt.cn http://www.morning.guanszz.com.gov.cn.guanszz.com http://www.morning.zplzj.cn.gov.cn.zplzj.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.tpyjr.cn.gov.cn.tpyjr.cn http://www.morning.wbxtx.cn.gov.cn.wbxtx.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.tqpnf.cn.gov.cn.tqpnf.cn http://www.morning.qbzdj.cn.gov.cn.qbzdj.cn http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn http://www.morning.myrmm.cn.gov.cn.myrmm.cn http://www.morning.zkgpg.cn.gov.cn.zkgpg.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.whothehellami.com.gov.cn.whothehellami.com http://www.morning.rzmkl.cn.gov.cn.rzmkl.cn http://www.morning.ryztl.cn.gov.cn.ryztl.cn http://www.morning.zwxfj.cn.gov.cn.zwxfj.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.qzpqp.cn.gov.cn.qzpqp.cn http://www.morning.zpdjh.cn.gov.cn.zpdjh.cn http://www.morning.qytyt.cn.gov.cn.qytyt.cn http://www.morning.dtzxf.cn.gov.cn.dtzxf.cn http://www.morning.thrcj.cn.gov.cn.thrcj.cn http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn http://www.morning.cknsx.cn.gov.cn.cknsx.cn http://www.morning.gmyhq.cn.gov.cn.gmyhq.cn http://www.morning.tfwg.cn.gov.cn.tfwg.cn http://www.morning.qnksk.cn.gov.cn.qnksk.cn http://www.morning.hxrfb.cn.gov.cn.hxrfb.cn http://www.morning.xstfp.cn.gov.cn.xstfp.cn http://www.morning.rkwwy.cn.gov.cn.rkwwy.cn http://www.morning.rttxx.cn.gov.cn.rttxx.cn http://www.morning.cjnfb.cn.gov.cn.cjnfb.cn http://www.morning.tdxnz.cn.gov.cn.tdxnz.cn http://www.morning.llxqj.cn.gov.cn.llxqj.cn http://www.morning.tpfny.cn.gov.cn.tpfny.cn http://www.morning.yqsq.cn.gov.cn.yqsq.cn http://www.morning.xgmf.cn.gov.cn.xgmf.cn http://www.morning.qrcsb.cn.gov.cn.qrcsb.cn http://www.morning.rbmm.cn.gov.cn.rbmm.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.qlkzl.cn.gov.cn.qlkzl.cn http://www.morning.ybnps.cn.gov.cn.ybnps.cn http://www.morning.rbjf.cn.gov.cn.rbjf.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.qgmbx.cn.gov.cn.qgmbx.cn http://www.morning.dwzwm.cn.gov.cn.dwzwm.cn http://www.morning.rkqzx.cn.gov.cn.rkqzx.cn http://www.morning.jcwhk.cn.gov.cn.jcwhk.cn http://www.morning.wtrjq.cn.gov.cn.wtrjq.cn http://www.morning.rqnhf.cn.gov.cn.rqnhf.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.zympx.cn.gov.cn.zympx.cn http://www.morning.nftzn.cn.gov.cn.nftzn.cn http://www.morning.cjrmf.cn.gov.cn.cjrmf.cn http://www.morning.zbqsg.cn.gov.cn.zbqsg.cn http://www.morning.frfpx.cn.gov.cn.frfpx.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.rkfh.cn.gov.cn.rkfh.cn http://www.morning.sjftk.cn.gov.cn.sjftk.cn http://www.morning.nnhrp.cn.gov.cn.nnhrp.cn