import java.sql.*;
public class DBConnection //连接库的beanjsp页需要操作数据添加删除可直接 DBConnection db = new DBConnection(); 然后db.****();
{
String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=数据库名";
Connection conn=null;
Statement state=null;
ResultSet rs=null;
String flag = "";
boolean barflag=false;
String dbname="sa";
String dbpwd="sa";
public DBConnection()
{
try{
Class.forName(driver);
conn=DriverManager.getConnection(url,dbname,dbpwd);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public void update(String sql)
{
try
{
state=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
state.executeUpdate(sql);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public ResultSet query(String sql)
{
try
{
state=conn.createStatement();
rs=state.executeQuery(sql);
}
catch(Exception e)
{
e.printStackTrace();
}
return rs;
}
}