jsp mysql 网上书店源代码
2个回答
展开全部
这些都是我日常用到的类(mysql)
数据库连接类(也叫数据池)
package DBConnection;
import java.sql.*;
public class DBConnectionManager {
private String driverName="com.mysql.jdbc.Driver";
private String url="jdbc:MySQL://localhost:3306/service?useUnicode=true&characterEncoding=GBK";
private String user="root";
private String password="lena";
public void setManager(String newDriverName,String newUrl,String newUser,String newPassword){
driverName=newDriverName;
url=newUrl;
user=newUser;
password=newPassword;
}
public void setDriverName(String newDriverName) {
driverName = newDriverName;
}
public String getDriverName() {
return driverName;
}
public void setUrl(String newUrl) {
url = newUrl;
}
public String getUrl() {
return url;
}
public void setUser(String newUser) {
user = newUser;
}
public String getUser() {
return user;
}
public void setPassword(String newPassword) {
password = newPassword;
}
public String getPassword() {
return password;
}
public Connection getConnection(){
try{
Class.forName(driverName);
return DriverManager.getConnection(url,user,password);
}
catch(Exception e){e.printStackTrace();
return null;
}
}
}
数据库操作类:
package DBConnection;
import java.sql.*;
import DBConnection.DBConnectionManager;
public class DataBase {
protected Connection conn=null;
protected Statement stmt=null;
protected ResultSet rs=null;
protected PreparedStatement pstmt=null;
protected String sql;
protected boolean isConnect=true;
public DataBase(){
try{
sql="";
DBConnectionManager dcm=new DBConnectionManager();
conn=dcm.getConnection();
stmt=conn.createStatement();
}
catch(Exception e){
System.out.println(e);
isConnect=false;
}
}
public Statement getStatement() {
return stmt;
}
public Connection getConnection() {
return conn;
}
public PreparedStatement getPreparedStatement() {
return pstmt;
}
public ResultSet getResultSet() {
return rs;
}
public String getSql() {
return sql;
}
public boolean execute() throws Exception {
return false;
}
public boolean insert() throws Exception {
return false;
}
public boolean update() throws Exception {
return false;
}
public boolean delete() throws Exception {
return false;
}
public boolean query() throws Exception {
return false;
}
public void close() throws SQLException {
if ( stmt != null )
{
stmt.close();
stmt = null;
}
conn.close();
conn = null;
}
}
这个是我自己设计的我要用的类,使用很方便
package DBConnection;
import DBConnection.DataBase;
import java.sql.*;
public class data extends DataBase{
public data(){super();}
public ResultSet select(String sqlString){
try{
rs=stmt.executeQuery(sqlString);
return rs;
}
catch(SQLException e){
System.out.println(e);
return null;
}
}
public boolean edit(String sqlString){
try{
stmt.execute(sqlString);
System.out.println("update success");
return true;
}
catch(SQLException e){
System.out.println(e);
return false;
}
}
public String codeToString(String str)
{//处理中文字符串的函数
try
{
str = new String(str.getBytes("ISO-8859-1"),"GBK");
return str;
}
catch (Exception e)
{
return str;
}
}
}
数据库连接类(也叫数据池)
package DBConnection;
import java.sql.*;
public class DBConnectionManager {
private String driverName="com.mysql.jdbc.Driver";
private String url="jdbc:MySQL://localhost:3306/service?useUnicode=true&characterEncoding=GBK";
private String user="root";
private String password="lena";
public void setManager(String newDriverName,String newUrl,String newUser,String newPassword){
driverName=newDriverName;
url=newUrl;
user=newUser;
password=newPassword;
}
public void setDriverName(String newDriverName) {
driverName = newDriverName;
}
public String getDriverName() {
return driverName;
}
public void setUrl(String newUrl) {
url = newUrl;
}
public String getUrl() {
return url;
}
public void setUser(String newUser) {
user = newUser;
}
public String getUser() {
return user;
}
public void setPassword(String newPassword) {
password = newPassword;
}
public String getPassword() {
return password;
}
public Connection getConnection(){
try{
Class.forName(driverName);
return DriverManager.getConnection(url,user,password);
}
catch(Exception e){e.printStackTrace();
return null;
}
}
}
数据库操作类:
package DBConnection;
import java.sql.*;
import DBConnection.DBConnectionManager;
public class DataBase {
protected Connection conn=null;
protected Statement stmt=null;
protected ResultSet rs=null;
protected PreparedStatement pstmt=null;
protected String sql;
protected boolean isConnect=true;
public DataBase(){
try{
sql="";
DBConnectionManager dcm=new DBConnectionManager();
conn=dcm.getConnection();
stmt=conn.createStatement();
}
catch(Exception e){
System.out.println(e);
isConnect=false;
}
}
public Statement getStatement() {
return stmt;
}
public Connection getConnection() {
return conn;
}
public PreparedStatement getPreparedStatement() {
return pstmt;
}
public ResultSet getResultSet() {
return rs;
}
public String getSql() {
return sql;
}
public boolean execute() throws Exception {
return false;
}
public boolean insert() throws Exception {
return false;
}
public boolean update() throws Exception {
return false;
}
public boolean delete() throws Exception {
return false;
}
public boolean query() throws Exception {
return false;
}
public void close() throws SQLException {
if ( stmt != null )
{
stmt.close();
stmt = null;
}
conn.close();
conn = null;
}
}
这个是我自己设计的我要用的类,使用很方便
package DBConnection;
import DBConnection.DataBase;
import java.sql.*;
public class data extends DataBase{
public data(){super();}
public ResultSet select(String sqlString){
try{
rs=stmt.executeQuery(sqlString);
return rs;
}
catch(SQLException e){
System.out.println(e);
return null;
}
}
public boolean edit(String sqlString){
try{
stmt.execute(sqlString);
System.out.println("update success");
return true;
}
catch(SQLException e){
System.out.println(e);
return false;
}
}
public String codeToString(String str)
{//处理中文字符串的函数
try
{
str = new String(str.getBytes("ISO-8859-1"),"GBK");
return str;
}
catch (Exception e)
{
return str;
}
}
}
广东轻亿云软件
2024-05-14 广告
2024-05-14 广告
广东轻亿云软件科技有限公司在软件开发领域深耕多年,积累了丰富的经验和技术实力。我们深知API接口在现代软件开发中的重要性,因此,我们与多家业界领先的API接口提供商保持着紧密的合作关系,确保我们的产品和服务能够充分利用这些接口,为用户提供更...
点击进入详情页
本回答由广东轻亿云软件提供
展开全部
package studentInfo;
/* 数据访问组件 */
import java.io.*;
import java.util.*;
import java.sql.*;
public class DBPool{
private static DBPool instance = null;
//取得连接
public static synchronized Connection getConnection() {
if (instance == null){
instance = new DBPool();
}
return instance._getConnection();
}
private DBPool(){
super();
}
private Connection _getConnection(){
try{
String sDBDriver = null;
String sConnection = null;
String sUser = null;
String sPassword = null;
Properties p = new Properties();
InputStream is = getClass().getResourceAsStream("/db.properties");
p.load(is);
sDBDriver = p.getProperty("DBDriver",sDBDriver);
sConnection = p.getProperty("Connection",sConnection);
sUser = p.getProperty("User","");
sPassword = p.getProperty("Password","");
Properties pr = new Properties();
pr.put("user",sUser);
pr.put("password",sPassword);
pr.put("characterEncoding", "GB2312");
pr.put("useUnicode", "TRUE");
Class.forName(sDBDriver).newInstance();
return DriverManager.getConnection(sConnection,pr);
}
catch(Exception se){
System.out.println(se);
return null;
}
}
//释放资源
public static void dbClose(Connection conn,PreparedStatement ps,ResultSet rs)
throws SQLException
{
rs.close();
ps.close();
conn.close();
}
}
/* 数据访问组件 */
import java.io.*;
import java.util.*;
import java.sql.*;
public class DBPool{
private static DBPool instance = null;
//取得连接
public static synchronized Connection getConnection() {
if (instance == null){
instance = new DBPool();
}
return instance._getConnection();
}
private DBPool(){
super();
}
private Connection _getConnection(){
try{
String sDBDriver = null;
String sConnection = null;
String sUser = null;
String sPassword = null;
Properties p = new Properties();
InputStream is = getClass().getResourceAsStream("/db.properties");
p.load(is);
sDBDriver = p.getProperty("DBDriver",sDBDriver);
sConnection = p.getProperty("Connection",sConnection);
sUser = p.getProperty("User","");
sPassword = p.getProperty("Password","");
Properties pr = new Properties();
pr.put("user",sUser);
pr.put("password",sPassword);
pr.put("characterEncoding", "GB2312");
pr.put("useUnicode", "TRUE");
Class.forName(sDBDriver).newInstance();
return DriverManager.getConnection(sConnection,pr);
}
catch(Exception se){
System.out.println(se);
return null;
}
}
//释放资源
public static void dbClose(Connection conn,PreparedStatement ps,ResultSet rs)
throws SQLException
{
rs.close();
ps.close();
conn.close();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询