求基于 JSP和javabean技术 用JDBC访问sqlserver 或mysql的 网上商城系统 急需毕业论文和源码 !!非常感谢 30
邮箱1091520426@qq.comJSP技术就行~~使用JDBC访问sqlserver或mysql数据库...
邮箱1091520426@qq.com
JSP技术就行~~ 使用JDBC访问sqlserver或mysql数据库 展开
JSP技术就行~~ 使用JDBC访问sqlserver或mysql数据库 展开
3个回答
展开全部
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<String> 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<String> 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<String> 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));
}
}
}
}
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<String> 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<String> 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<String> 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));
}
}
}
}
追问
???
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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!");
// }
}
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!");
// }
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2011-05-18
展开全部
又到毕业时啊。
追问
是的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询