JSP SQL数据链接类调用问题
我写了个数据库的链接类
package db;
import java.sql.*;
public class ConnDB
{
public Connection conn=null;
public Statement st=null;
public String bd_Drive= "com.microsoft.jdbc.sqlserver.SQLServerDriver ";
public Connection mygetConn() throws Exception{
Class.forName(bd_Drive);
String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db ";
String user= "sa ";
String pwd= " ";
conn=DriverManager.getConnection(url,user,pwd);
st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
return conn;
}
public void getConn()throws Exception
{
Class.forName(bd_Drive);
String url= "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=db ";
String user= "sa ";
String pwd= " ";
conn=DriverManager.getConnection(url,user,pwd);
}
public ResultSet getRs(String sql)throws Exception
{
st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs=st.executeQuery(sql);
return rs;
}
public void getUpdate(String sql)throws Exception
{
st=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
st.executeUpdate(sql);
}
public void closeConn() throws Exception
{
if(st!=null)
st.close();
st=null;
if(conn!=null)
conn.close();
conn=null;
}
}
文件调用:
package com;
import java.sql.*;
import db.*;
import db.ConnDB;
public class newManage
{
ConnDB con= new ConnDB();
private String userid,password,qy,username,bm,job,type;
public void setUserid(String u){userid=u;}
public String getUserid(){return userid;}
public void setPassword(String p){password=p;}
public String getPassword(){return password;}
public void setQy(String q){qy=q;}
public String getQy(){return qy;}
public void setUsername(String n){username=n;}
public String getUsername(){return username;}
public void setBm(String b){bm=b;}
public String getBm(){return bm;}
public void setJob(String j){job=j;}
public String getJob(){return job;}
public void setType(String t){type=t;}
public String getType(){return type;}
public void updateDatabase(){
con.mygetConn();
String sql= "Insert Into Manages(userid,[password],qy,username,job,bm,type) values(?,?,?,?,?,?,?) ";
PreparedStatement perstmt=conn.prepareStatement(sql);
perstmt.setString(1,userid);
perstmt.setString(2,password);
perstmt.setString(3,qy);
perstmt.setString(4,username);
perstmt.setString(5,job);
perstmt.setString(6,bm);
perstmt.setString(7,type);
perstmt.executeUpdate();
}
}
问题 :PreparedStatement perstmt=conn.prepareStatement(sql);
这行报错,conn,未定义,请高手帮忙.我是想通过调用 con.mygetConn();后有个conn可以使用.
[解决办法]
public void updateDatabase(){
Connection conn=null;
try {
conn=con.mygetConn();
String sql= "Insert Into Manages(userid,[password],qy,username,job,bm,type) values(?,?,?,?,?,?,?) ";
PreparedStatement perstmt=conn.prepareStatement(sql);
perstmt.setString(1,userid);
perstmt.setString(2,password);
perstmt.setString(3,qy);
perstmt.setString(4,username);
perstmt.setString(5,job);
perstmt.setString(6,bm);
perstmt.setString(7,type);
perstmt.executeUpdate();
}catch(Exception e) {
e.printStackTrace();
}
}