3个回答
展开全部
DBConnectionManager.java
import java.sql.*;
public class DBConnectionManager {
private String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo";
private String user="sa";
private String password="";
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Connection getConnection(){
try{
Class.forName(driverName);
return DriverManager.getConnection(url, user, password);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
DBSQLManager.java
import java.sql.*;
public class DBSQLManager {
protected Connection con=null;//Connection对象
protected Statement stmt=null;//Statement对象
protected ResultSet rs=null;//记录结果集
protected String sql=""; //SQL语句
public DBSQLManager(){
try {
DBConnectionManager dcm=new DBConnectionManager();
con=dcm.getConnection();
//con.setAutoCommit(false);//添加事物,既是否自动提交
stmt=con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Statement getStmt(){
return stmt;
}
public Connection getCon(){
return con;
}
public ResultSet getRs(){
return rs;
}
public void setSql(String sql){
this.sql=sql;
}
public String getSql(){
return sql;
}
//查找
public void execueQuery(){
try {
rs=stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
//更新
public void executeUpdate(){
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭
public void close(){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs=null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt=null;
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con=null;
}
}
如果楼主有不明白的,,百度HI我
祝楼主早日成功!
import java.sql.*;
public class DBConnectionManager {
private String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo";
private String user="sa";
private String password="";
public String getDriverName() {
return driverName;
}
public void setDriverName(String driverName) {
this.driverName = driverName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public Connection getConnection(){
try{
Class.forName(driverName);
return DriverManager.getConnection(url, user, password);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
DBSQLManager.java
import java.sql.*;
public class DBSQLManager {
protected Connection con=null;//Connection对象
protected Statement stmt=null;//Statement对象
protected ResultSet rs=null;//记录结果集
protected String sql=""; //SQL语句
public DBSQLManager(){
try {
DBConnectionManager dcm=new DBConnectionManager();
con=dcm.getConnection();
//con.setAutoCommit(false);//添加事物,既是否自动提交
stmt=con.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Statement getStmt(){
return stmt;
}
public Connection getCon(){
return con;
}
public ResultSet getRs(){
return rs;
}
public void setSql(String sql){
this.sql=sql;
}
public String getSql(){
return sql;
}
//查找
public void execueQuery(){
try {
rs=stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
//更新
public void executeUpdate(){
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
//关闭
public void close(){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs=null;
}
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt=null;
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con=null;
}
}
如果楼主有不明白的,,百度HI我
祝楼主早日成功!
展开全部
建立一个java类,里面封装了与数据库到连接,对外提供接口(方法),只要在jsp里调用就可以了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
封装到bean里面有点麻烦,等于自己实现简易orm了,况且多表查询时还不好操作,把结果集放到List里面还好
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询