用myeclipse做jsp简单的图书管理系统,通过javabean连接mysql怎么连接啊, 新手小白在线等
我的javabeanpackagetushu_bean;importjava.sql.*;publicclassMySqlCon{privateStringDbName=...
我的javabean
package tushu_bean;
import java.sql.*;
public class MySqlCon
{
private String DbName = "";
private String pass = "";
private String url = "";
private Connection con = null;
private Statement stmt = null;
private ResultSet rs = null;
public MySqlCon()
{
}
public void setDbName (String name)
{
this.DbName = name;
}
public void setPass (String pass)
{
this.pass = pass;
}
public void BulidCon()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
url ="jdbc:mysql://localhost/"+DbName+"?user=root&password="+pass+"&useUnicode=true&characterEncoding=gb2312";
con= DriverManager.getConnection(url);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch (Exception e)
{
}
}
public ResultSet selectLog(String sql)
{
try
{
BulidCon();
rs = stmt.executeQuery(sql);
}
catch(Exception e)
{
System.out.println(e.toString());
}
return rs;
}
public void updateLog(String sql)
{
try
{
BulidCon();
stmt.executeUpdate(sql);
}
catch (Exception e)
{
}
}
public void close()
{
try
{
con.close();
stmt.close();
}
catch (SQLException e)
{
}
}
//执行查询类的SQL语句,有返回集
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}
catch(SQLException e)
{
System.err.println("Data.executeQuery:"+e.getMessage());
}
return rs;
}
//执行更新类的SQL语句,无返回集
public void executeUpdate(String sql)
{
stmt=null;
rs=null;
try
{
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
stmt.close();
con.close();
}
catch(SQLException e)
{
System.err.println("Data.executeQuery:"+e.getMessage());
}
}
//关闭对象
public void closeStmt()
{
try
{
stmt.close();
}
catch(SQLException e)
{
System.err.println("Data.executeQuery:"+e.getMessage());
}
}
public void closeConn()
{
try
{
con.close();
}
catch(SQLException e)
{
System.err.println("Data.executeQuery:"+e.getMessage());
}
}
}
很急,可加QQ,解决问题给分 展开
package tushu_bean;
import java.sql.*;
public class MySqlCon
{
private String DbName = "";
private String pass = "";
private String url = "";
private Connection con = null;
private Statement stmt = null;
private ResultSet rs = null;
public MySqlCon()
{
}
public void setDbName (String name)
{
this.DbName = name;
}
public void setPass (String pass)
{
this.pass = pass;
}
public void BulidCon()
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
url ="jdbc:mysql://localhost/"+DbName+"?user=root&password="+pass+"&useUnicode=true&characterEncoding=gb2312";
con= DriverManager.getConnection(url);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch (Exception e)
{
}
}
public ResultSet selectLog(String sql)
{
try
{
BulidCon();
rs = stmt.executeQuery(sql);
}
catch(Exception e)
{
System.out.println(e.toString());
}
return rs;
}
public void updateLog(String sql)
{
try
{
BulidCon();
stmt.executeUpdate(sql);
}
catch (Exception e)
{
}
}
public void close()
{
try
{
con.close();
stmt.close();
}
catch (SQLException e)
{
}
}
//执行查询类的SQL语句,有返回集
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
}
catch(SQLException e)
{
System.err.println("Data.executeQuery:"+e.getMessage());
}
return rs;
}
//执行更新类的SQL语句,无返回集
public void executeUpdate(String sql)
{
stmt=null;
rs=null;
try
{
stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
stmt.executeUpdate(sql);
stmt.close();
con.close();
}
catch(SQLException e)
{
System.err.println("Data.executeQuery:"+e.getMessage());
}
}
//关闭对象
public void closeStmt()
{
try
{
stmt.close();
}
catch(SQLException e)
{
System.err.println("Data.executeQuery:"+e.getMessage());
}
}
public void closeConn()
{
try
{
con.close();
}
catch(SQLException e)
{
System.err.println("Data.executeQuery:"+e.getMessage());
}
}
}
很急,可加QQ,解决问题给分 展开
1个回答
展开全部
javabean是和数据库中的数据表对应的类,连接数据库要用到jdbc,如:
public class DBUtils {
public static Connection getDBConnection(){
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库?" +
"useUnicode=true&characterEncoding=GBK","root","密码");
}catch(Exception e){
e.printStackTrace();
System.out.println("Error!Fail to connection DB!");
}
return conn;
}
这个类的的getDBConnection方法返回就是一个数据库连接,通过这个连接就可以操作数据库了
你可以添加其他方法来方便操作数据库~~
注意的是com.mysql.jdbc.Driver这个类一定要放到classpath中
public class DBUtils {
public static Connection getDBConnection(){
Connection conn = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/数据库?" +
"useUnicode=true&characterEncoding=GBK","root","密码");
}catch(Exception e){
e.printStackTrace();
System.out.println("Error!Fail to connection DB!");
}
return conn;
}
这个类的的getDBConnection方法返回就是一个数据库连接,通过这个连接就可以操作数据库了
你可以添加其他方法来方便操作数据库~~
注意的是com.mysql.jdbc.Driver这个类一定要放到classpath中
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询