package com.bbs.db;
import java.sql.Connection;
import java.sql.Date;
//import java.sql.DriverManager;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
public class DBBean {
//private String dbDriver="sun.jdbc.odbc.JdbcOdbcDriver";
private String dbDriver="com.mysql.jdbc.Driver";
//private String url="jdbc:odbc:forum";
private String url="jdbc:mysql://localhost/bbs";
//private String user="";
//private String pwd="";
private String user="root";
private String pwd="root";
private Connection conn=null;
private Statement stmt = null;
ResultSet rs = null;
public DBBean()
{
try
{
Class.forName(dbDriver).newInstance();
//conn=DriverManager.getConnection(url,"root","root");
}catch(Exception e)
{
e.printStackTrace();
}
}
public Connection getDBBean() {
return conn;
}
public void close(){
try
{
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public ResultSet executeQuery(String sql)
{
try {
conn = DriverManager.getConnection(url,user,pwd);
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.out.println(ex.getMessage());
}
return rs;
}
public int executeUpdate(String sql)
{
int result=0;
try{
conn = DriverManager.getConnection(url,user,pwd);
stmt = conn.createStatement();
result = stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.out.println(ex.getMessage());
}
return result;
}
/*private Connection conn;
private Statement stmt;
private PreparedStatement pstmt;
private ResultSet rs;
private void init(){//创建数据库的一个 Connection连接
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url="jdbc:mysql://localhost/bbs";
conn=DriverManager.getConnection(url,"root","root");
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public DBBean(){
init();
try {
stmt=conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void executeQuery(String s){
try {
if(stmt!=null){
rs=stmt.executeQuery(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int executeUpdate(String s){
int status=0;
try {
if(stmt!=null){
status=stmt.executeUpdate(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return status;
}
//以下为赋值方法
public void setString(int i,String s){
try {
pstmt.setString(i,s);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setBoolean(int i,boolean flag){
try {
pstmt.setBoolean(i,flag);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setDate(int i,Date date){
try {
pstmt.setDate(i, date);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setTime(int i,Time time){
try {
pstmt.setTime(i, time);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setShort(int i,short word0){
try {
pstmt.setShort(i, word0);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setInt(int i,int j){
try {
pstmt.setInt(i, j);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setLong(int i,long l){
try {
pstmt.setLong(i, l);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setFloat(int i,float f){
try {
pstmt.setFloat(i, f);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setDouble(int i,double d){
try {
pstmt.setDouble(i, d);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//以下为取值方法
public boolean getBoolean(int i) throws Exception{//取得布尔类型的字段,通过列号
return rs.getBoolean(i);
}
public boolean getBoolean(String s) throws Exception{//取得布尔类型的字段,通过字段名
return rs.getBoolean(s);
}
public Date getDate(int i) throws Exception{
return rs.getDate(i);
}
public Date getDate(String s) throws Exception{
return rs.getDate(s);
}
public Time getTime(int i) throws Exception{
return rs.getTime(i);
}
public Time getTime(String s) throws Exception{
return rs.getTime(s);
}
public double getDouble(int i) throws Exception{
return rs.getDouble(i);
}
public double getDouble(String s) throws Exception{
return rs.getDouble(s);
}
public float getFloat(int i) throws Exception{
return rs.getFloat(i);
}
public float getFloat(String s) throws Exception{
return rs.getFloat(s);
}
public int getInt(int i) throws Exception{
return rs.getInt(i);
}
public int getInt(String s) throws Exception{
return rs.getInt(s);
}
public long getLong(int i) throws Exception{
return rs.getLong(i);
}
public long getLong(String s) throws Exception{
return rs.getLong(s);
}
public short getShort(int i) throws Exception{
return rs.getShort(i);
}
public short getShort(String s) throws Exception{
return rs.getShort(s);
}
public String getString(int i) throws Exception{
return rs.getString(i);
}
public String getString(String s) throws Exception{
return rs.getString(s);
}
public boolean next(){
try {
return rs.next();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
public void close(){
try {
if(rs!=null) rs.close();
if(stmt!=null) stmt.close();
if(conn!=null) conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}*/
}