文件名称:数据库连接,里面有各种数据库的连接方法
文件大小:4KB
文件格式:JAVA
更新时间:2013-01-12 02:29:47
这是一个关于数据库连接的代码,有用的着的来看看
DBConnectionManager.java //连接数据库用的 import java.sql.*; public class DBConnectionManager { private String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver"; private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo"; private String user="sa"; private String password=""; public String getDriverName() { return driverName; } public void setDriverName(String driverName) { this.driverName = driverName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public Connection getConnection(){ try{ Class.forName(driverName); return DriverManager.getConnection(url, user, password); }catch(Exception e){ e.printStackTrace(); return null; } } } DBSQLManager.java //操作数据库用的 import java.sql.*; public class DBSQLManager { protected Connection con=null;//Connection对象 protected Statement stmt=null;//Statement对象 protected ResultSet rs=null;//记录结果集 protected String sql=""; //SQL语句 public DBSQLManager(){ try { DBConnectionManager dcm=new DBConnectionManager(); con=dcm.getConnection(); //con.setAutoCommit(false);//添加事物,既是否自动提交 stmt=con.createStatement(); } catch (SQLException e) { e.printStackTrace(); } } public Statement getStmt(){ return stmt; } public Connection getCon(){ return con; } public ResultSet getRs(){ return rs; } public void setSql(String sql){ this.sql=sql; } public String getSql(){ return sql; } //查找 public void execueQuery(){ try { rs=stmt.executeQuery(sql); } catch (SQLException e) { e.printStackTrace(); } } //更新 public void executeUpdate(){ try { stmt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } } //关闭 public void close(){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs=null; } if(stmt!=null){ try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } stmt=null; } try { con.close(); } catch (SQLException e) { e.printStackTrace(); } con=null; } } SqlOperate.java //用来调用数据库操作语句 import java.sql.ResultSet; import java.sql.SQLException; public class SqlOperate { //插入,修改,删除 public void insOrModOrDel(String sql){ DBSQLManager dbsm=new DBSQLManager(); dbsm.getStmt(); dbsm.setSql(sql); dbsm.executeUpdate(); dbsm.close(); } //显示 public void display(String sql){ DBSQLManager dbsm=new DBSQLManager(); dbsm.getStmt(); dbsm.setSql(sql); dbsm.execueQuery(); ResultSet rs=dbsm.getRs(); try { while(rs!=null&&rs.next()){ System.out.print(rs.getObject(1)+"\t"); System.out.print(rs.getObject(2)+"\t"); System.out.print(rs.getObject(3)+"\t"); System.out.println(rs.getObject(4)); } dbsm.close(); } catch (SQLException e) { e.printStackTrace(); } } } SqlMain.java //数据库的测试主函数 public class SqlMain { public static void main(String[] args){ SqlOperate sqlOpt=new SqlOperate(); // sqlOpt.insOrModOrDel("insert into user1 values('qianhaifei',999999,'qianhaifei@163.com')");//插入 // sqlOpt.insOrModOrDel("update user1 set username='weixiangyang' where id=4");//修改 // sqlOpt.insOrModOrDel("delete from user1 where username='weixy2000'");//删除 sqlOpt.display("select *from user1");//显示 } }