求基于 JSP和javabean技术 用JDBC访问sqlserver 或mysql的 网上商城系统 急需毕业论文和源码 !!非常感谢

2024-12-17 01:10:49
推荐回答(3个)
回答1:

package dataBase;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DBHelper {
private Connection con;
private PreparedStatement pstmt;
private ResultSet rs;
//静态语句块,加载驱动
static {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

public ArrayList findBySql(String sql, ArrayList params, Class c) {
con = this.getCon();
try {
pstmt=con.prepareStatement(sql);
doParams(pstmt, params);
rs=pstmt.executeQuery();
while(rs.next()){
/**************************************************/
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return List;
}
//连接数据库
public Connection getCon() {
try {
con = DriverManager.getConnection(
"jdbc:sqlserver://192.168.2.30:1433;databaseName=数据库名",
"连接数据库名", "密码");
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
//关闭所有连接
public void closeAll(ResultSet rs, PreparedStatement pstmt, Connection con) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//对数据进行增、删、改
// insert into xx values(?,?); params={"张三","a"};
public void doUpdate(String sql, List params) {
con = this.getCon();
try {
pstmt = con.prepareStatement(sql);
doParams(pstmt, params);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeAll(null, pstmt, con);
}
}
public void doParams(PreparedStatement pstmt, List params)
throws SQLException {
if (pstmt != null && params != null && params.size() > 0) {
for (int i = 0; i < params.size(); i++) {
pstmt.setString(i + 1, params.get(i));
}
}
}
}

回答2:

package JavaBean;
import java.sql.*;
public class DatabaseConn {
String sDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String sContr="jdbc:odbc:Library";//Library 为数据源的名字,建议最好配置的时候与数据库的 名字相同
Connection conn=null;
ResultSet rs=null;
public DatabaseConn(){
try{
Class.forName(sDBDriver);//加载驱动器

}
catch(ClassNotFoundException e){
System.out.print(e.getMessage());
}
}
public ResultSet executeQuery(String sql){//用于查询时调用
try{
conn=DriverManager.getConnection(sContr,"","");
Statement stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
}
catch(SQLException e){
System.out.print(e.getMessage());
}
return rs;
}
public int executeUpdate(String sql) throws SQLException{//用于增、删、改时调用

int num=0;
conn=DriverManager.getConnection(sContr,"","");
Statement stmt=conn.createStatement();
num=stmt.executeUpdate(sql);
return num;
}
// 测试数据库的链接
// public static void main(String args[]){
// DatabaseConn conn=new DatabaseConn();
// System.out.println("Connection Success!");
// }
}

回答3:

又到毕业时啊。