上一篇把基本的JDBC连接写了出来,但是你们会发现有很多重复的地方,这些重复的地方将他们打包至一个地方然后反射到主类文件即可达到优化效果!
1.写pro.txt文件(该文件是写出主文件重复的密码账户,数据库具体地址等信息)
user=root
password=samlin123
url=jdbc:mysql:///test_jdbc
(注意:千万不要加上任何一句多余的话,也不要把java语法带到这里来,否则运行会不成功)
2.编写jdbcUtil工具(该文件写出主文件中连接,释放资源的重复语句,(其中使用FileInputStream和Properties共同获取文件信息))
package JDBC_up;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class jdbcUtil {
private static String url=null;
private static String user=null;
private static String password=null;
private static Strign driver=null;
static{
Properties pro=new Properties();
try {
FileInputStream fis=new FileInputStream("./src/JDBC_up/pro.txt");//还可以通过 当前类.class.getResourceAsstream('/JDBC_up/pro.txt') (其实这种方法更为通用!!!!)
pro.load(fis);
} catch (IOException e) {
e.printStackTrace();
}
url = pro.getProperty("url");
user = pro.getProperty("user");
password=pro.getProperty("password");
driver=pro.getProperty("driver");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接资源
public static Connection getConnection() throws Exception {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
//释放资源
public static void close(Connection conn,PreparedStatement pstmt) throws Exception {
if(pstmt!=null){
pstmt.close();
}
if(conn!=null){
conn.close();
}
}
public static void close(Connection conn,PreparedStatement pstmt,ResultSet rs) throws Exception {
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(conn!=null){
conn.close();
}
}
}
三,JDBC主类文件,功能:增删改查
package JDBC_up;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Jdbc_all {
public static void main(String[] args) throws Exception {
Connection conn = jdbcUtil.getConnection();
//增
String sql="insert into cba (id,player,salary) value(?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 2);
pstmt.setString(2, "易建联");
pstmt.setString(3, "60000000");
pstmt.executeUpdate();
//删
String sql1="delete from cba where id=?";
PreparedStatement pstmt1=conn.prepareStatement(sql1);
pstmt1.setInt(1, 2);
pstmt1.executeUpdate();
//改
String sql2="update cba set player=? where id=?";
PreparedStatement pstmt2=conn.prepareStatement(sql2);
pstmt2.setString(1, "姚明");
pstmt2.setInt(2,8);
pstmt2.executeUpdate();
//查
String sql3="select * from cba";
PreparedStatement pstmt3=conn.prepareStatement(sql3);
ResultSet rs = pstmt3.executeQuery(sql3);
while(rs.next()){
//获取字段1
int id = rs.getInt(1);
String player = rs.getString(2);
String salary = rs.getString(3);
System.out.println(id+player+salary);
}
jdbcUtil.close(conn, pstmt3);
}
}
编写jdbcutil时遇到的困难
1.properties文件一定要与jdbcUtil类一致.(user就不要打成username)
否则会导致:Access denied for user 'root1'@'localhost' (using password: YES)(一般来说using password:yes 指的是你有使用密码,但是密码不正确)
2.Access denied for user ''@'localhost' (using password: NO) 指的是你的账户,密码,Mysql地址不全面,密码根本用不上
3.优化JDBC,归纳出util工具的关键部分是:Class的getresourceAsStream和filestream联合起来取外部文件信息..
4.本人总是忘记写pstmt.setString(1,"易建联");或者pstmt.setint(1,1); 否则会导致 No value specified for parameter 1
5.java路径问题 fileinputstream的参数 ./ ---> 在普通的java项目当中(非web项目)代表项目根目录 一般./src这样表达
但是在web项目当中,则代表tomcat/bin目录
6.Connection conn = DriverManager.getConnection(url,user,password); 这个url 和 user 和 password 的顺序是固定的,不能颠倒
颠倒了则会报Access denied for user 'samlin123'@'localhost' (using password: YES)认为你的提交资料是全面的,但是你的密码不正确

本文介绍了一种通过创建配置文件和工具类来优化Java数据库连接(JDBC)的方法,包括如何减少代码冗余并提高可维护性。
1万+

被折叠的 条评论
为什么被折叠?



