1.打印输出conn检查conn是否为空,为空时对其做出判断,new一个connetion,
if (null==conn||conn.isClosed()) {
conn=DriverManager.getConnection(url, user, passwd);
}
2.数据库操作太频繁,不要每个数据库操作都new一个connection连接,我的操作是把connection类写成单利模式,并实时的操作不关闭connection,但是,
prepare statement和result set需要关闭,为了保险起见你也可以,在操作数据库的类中new一个全局的connection变量,每次操作传入connection,同时判断是否为空,为空时new一个;
package
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 OracleDBUtil {//单利模式的工具类,可能有所欠缺,也可自己写个
Connection conn = null;
private OracleDBUtil (){
}
private static OracleDBUtil instance=new OracleDBUtil();
public static OracleDBUtil getinstance(){
return instance;
}
public Connection getOracleConnection(){
Properties prop = new Properties();
try {
prop.load(new FileInputStream(“config.properties”));
} catch (IOException e1) {
e1.printStackTrace();
}
String driver=prop.getProperty(“driver”);
String url=prop.getProperty(“url”);
String user=prop.getProperty(“username”);
String passwd=prop.getProperty(“password”);
try {
if (driver==null) {
driver=prop.getProperty(“driver”);
}
Class.forName(driver);
if (null==conn||conn.isClosed()) {
conn=DriverManager.getConnection(url, user, passwd);
}
System.out.println(“oracle连接成功”);
} catch (SQLException e) {
e.printStackTrace();
System.out.println(“sql exception”+e.getMessage());
}catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println(“Driver class not found”+e.getMessage());
}
return conn;
}
public void closeConnection(Connection connection){
if (connection!=null) {
try {
if (!connection.isClosed()) {
connection.close();
System.out.println(“oracle已经断开”);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void closeConnection(ResultSet st,PreparedStatement pstme){
try {
if (st!=null) {
st.close();
}
if (pstme!=null) {
pstme.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
/*getOracleConnection();
closeConnection(conn);*/
}
}
以下为调用调用
Connection connconfig=OracleDBUtil.getinstance().getOracleConnection();//new一个
String cardIdkey=new OracleSql().Read(tablename,connconfig);//传入conn
public String Read(String tablename,Connection conn) throws SQLException{//得到身份证字段
try {
String sql=” XXXX “;
if (conn==null||conn .isClosed()) {
conn=OracleDBUtil.getinstance().getOracleConnection();
pstme = conn.prepareStatement(sql);
}else {
pstme = conn.prepareStatement(sql);
}
pstme.setString(1,tablename);
rs=pstme.executeQuery();
while (rs.next()) {
cardString= rs.getString(1);
System.out.println(“#######+”+cardString+rs.getString(2)+rs.getString(3)+rs.getString(4));
}
} catch (SQLException e) {
e.printStackTrace();
}
finally{
OracleDBUtil.getinstance().closeConnection(rs, pstme);
}
今天的文章conn=DriverManager.getConnection(url, user, passwd);报nullclassfound解决分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/28531.html