DBbean

import java.sql.*;
public class DBBean
{
private String driverStr = "com.mysql.jdbc.Driver";
private String connStr = "jdbc:mysql://localhost/context";
private String username = "";
private String password = "";
private Connection conn = null;
private Statement stmt = null;
public DBBean()
{
        try{
        Class.forName(driverStr);
        }
        catch(ClassNotFoundException ex)
{
System.out.println(ex.getMessage());
}
       
}
public void setDriverStr(String dstr)
{
driverStr=dstr;
}
public void setConnStr(String cstr)
{
connStr=cstr;
}
public void setUsername(String u)
{
username=u;
}
public void setPassword(String p)
{
password=p;
}
public ResultSet executeQuery(String sql)
{
ResultSet rs = null;
try {
conn = DriverManager.getConnection(connStr,username,password);
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(connStr,username,password);
stmt = conn.createStatement();
result = stmt.executeUpdate(sql);
}
catch(SQLException ex){
System.out.println(ex.getMessage());
}
return result;
}
public void close()
{
try{
stmt.close();
conn.close();
}
catch(SQLException ex){
System.out.println(ex.getMessage());
}
}
}

你可能感兴趣的:(sql,mysql,jdbc)