JdbcUtil类的编写
Posted web之家
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JdbcUtil类的编写相关的知识,希望对你有一定的参考价值。
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JdbcUtil {
public static final String url = "jdbc:mysql://localhost:3306/mydatabase";
public static final String name = "com.mysql.jdbc.Driver";
public static final String user = "root";
public static final String password = "12344";
public Connection conn = null;
public PreparedStatement pst = null;
public JdbcUtil(String sql) {
try {
Class.forName(name);//注册驱动
conn = DriverManager.getConnection(url, user, password);//获取连接
pst = conn.prepareStatement(sql);//准备执行语句
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
try {
this.conn.close();
this.pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
以上是关于JdbcUtil类的编写的主要内容,如果未能解决你的问题,请参考以下文章