java访问数据库的 javaBean

这是一个简单的java操作mysql数据库的例子,没有用到数据库连接池。

MysqlConnDB.java

Code:
  1. /*    
  2.  *访问mysql数据库 的 javaBean    
  3.  */     
  4. package com.classes;      
  5. import java.sql.*;      
  6. import java.io.*;      
  7. import java.util.*;      
  8. public class MysqlConnDB{      
  9.     public Connection conn = null;      
  10.     public ResultSet rs = null;      
  11.     public PreparedStatement pStmt = null;      
  12.     private static String dbDriver = "com.mysql.jdbc.Driver";      
  13.     private static String dbUrl = "jdbc:mysql://localhost:3306/myjavaqq?useUnicode=true&characterEncoding=utf8";      
  14.     private static String dbUserName = "root";      
  15.     private static String dbPwd = "isone";      
  16.     public static Connection getConnection(){      
  17.         Connection conn = null;      
  18.         try{      
  19.             MyProperties prop = new MyProperties("mysql.properties");      
  20.             dbDriver = prop.getProperty("dbDriver");      
  21.             String databaseName = prop.getProperty("databaseName");      
  22.             dbUserName = prop.getProperty("dbUserName");      
  23.             dbPwd = prop.getProperty("dbPassword");      
  24.             String address = prop.getProperty("address");      
  25.             String dbPort = prop.getProperty("dbPort");      
  26.             dbUrl = "jdbc:mysql://" + address + ":" + dbPort + "/" + databaseName + "?useUnicode=true&characterEncoding=utf8";      
  27.             //加载驱动      
  28.             Class.forName(dbDriver);      
  29.             //创建连接      
  30.             conn = DriverManager.getConnection(dbUrl,dbUserName,dbPwd);      
  31.         }      
  32.         catch(Exception e){      
  33.             e.printStackTrace();      
  34.         }      
  35.         if(conn==null){      
  36.             System.err.println("警告:数据库连接失败!");      
  37.         }      
  38.         return conn;      
  39.     }      
  40.           
  41.     //查询 操作      
  42.     public ResultSet doQuery(String sql,Vector v_parameter){      
  43.         try{      
  44.             conn = MysqlConnDB.getConnection();      
  45.             pStmt = conn.prepareStatement(sql);      
  46.             int parameterCount = v_parameter.size();      
  47.             for(int i=0;i<parameterCount;i++){      
  48.                 try{      
  49.                     pStmt.setString(i+1,(String)v_parameter.get(i));      
  50.                 }catch(Exception ex){      
  51.                     pStmt.setString(i+1,v_parameter.get(i).toString());      
  52.                 }      
  53.             }      
  54.             rs = pStmt.executeQuery();      
  55.         }      
  56.         catch(SQLException e){      
  57.             e.printStackTrace();      
  58.         }      
  59.         catch(Exception e){      
  60.             e.printStackTrace();      
  61.         }      
  62.         finally{      
  63.             try{      
  64.                 if(pStmt!=null){      
  65.                     pStmt.clearParameters();      
  66.                 }      
  67.             }catch(SQLException sqlex){      
  68.             }      
  69.         }      
  70.         return rs;      
  71.     }      
  72.           
  73.     //更新 插入 删除 操作      
  74.     public int doUpdate(String sql,Vector v_parameter){      
  75.         int result = 0;      
  76.         try{      
  77.             conn = MysqlConnDB.getConnection();      
  78.             pStmt = conn.prepareStatement(sql);      
  79.             int parameterCount = v_parameter.size();      
  80.             for(int i=0;i<parameterCount;i++){      
  81.                 try{      
  82.                     pStmt.setString(i+1,(String)v_parameter.get(i));      
  83.                 }catch(Exception ex){      
  84.                     pStmt.setString(i+1,v_parameter.get(i).toString());      
  85.                 }      
  86.             }      
  87.             result = pStmt.executeUpdate();      
  88.         }      
  89.         catch(SQLException e){      
  90.             e.printStackTrace();      
  91.             result = 0;      
  92.         }      
  93.         catch(Exception e){      
  94.             e.printStackTrace();      
  95.         }      
  96.         finally{      
  97.             try{      
  98.                 if(pStmt!=null){      
  99.                     pStmt.clearParameters();      
  100.                 }      
  101.             }catch(SQLException sqlex){      
  102.             }      
  103.         }      
  104.         return result;      
  105.     }      
  106.           
  107.     //关闭 操作      
  108.     public void closeConnection(){      
  109.         try{      
  110.             if(rs!=null){      
  111.                 rs.close();      
  112.             }      
  113.         }catch(Exception e){      
  114.             e.printStackTrace();      
  115.         }      
  116.               
  117.         try{      
  118.             if(pStmt != null){      
  119.                 pStmt.close();      
  120.             }      
  121.         }catch(Exception e){      
  122.             e.printStackTrace();      
  123.         }      
  124.               
  125.         try{      
  126.             if(conn != null){      
  127.                 conn.close();      
  128.             }      
  129.         }catch(Exception e){      
  130.             e.printStackTrace();      
  131.         }      
  132.     }      
  133. }    

读取配置的类MyProperties.java

Code:
  1. package com.classes;      
  2. import java.util.Properties;      
  3. import java.io.*;      
  4. public class MyProperties{      
  5.     Properties prop = null;      
  6.     public MyProperties(){      
  7.     }      
  8.           
  9.     public MyProperties(String pfileName){      
  10.         try{      
  11.             prop = new Properties();      
  12.             File file = new File(pfileName);      
  13.             FileInputStream finput = new FileInputStream(file);      
  14.             prop.load(finput);      
  15.             finput.close();      
  16.         }      
  17.         catch(FileNotFoundException fe){      
  18.             fe.printStackTrace();      
  19.         }      
  20.         catch(IOException ie){      
  21.             ie.printStackTrace();      
  22.         }      
  23.     }      
  24.           
  25.     public String getProperty(String key){      
  26.         //return new String(prop.getProperty(key).getBytes("Latin1"), "GBK");      
  27.         return prop.getProperty(key).trim();      
  28.     }      
  29. }    

配置文件mysql.properties

Code:
  1. #数据库配置      
  2. dbDriver = com.mysql.jdbc.Driver      
  3. databaseName = myjavaqq            
  4. dbUserName = root      
  5. dbPassword = isone      
  6. address = localhost      
  7. dbPort = 3306    

测试类ConnTest.java

Code:
  1. package com.classes;      
  2. import java.util.*;      
  3. import java.sql.*;      
  4. public class ConnTest{      
  5.     //写入数据      
  6.     MysqlConnDB connDB = null;      
  7.     Vector v_parameter = new Vector();      
  8.     String sqlString = "";      
  9.     int rowCount = -1;      
  10.     public ConnTest(){      
  11.         connDB = new MysqlConnDB();      
  12.     }      
  13.           
  14.     //创建表      
  15.     private void createTable(){      
  16.         sqlString = "CREATE TABLE `t_user` (`i_Id` int(10) unsigned NOT NULL auto_increment,`i_age` int(3) NOT NULL,";      
  17.         sqlString = sqlString +"`c_userName` char(10) NOT NULL, PRIMARY KEY (`i_Id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;";      
  18.         v_parameter.clear();      
  19.         rowCount = connDB.doUpdate(sqlString,v_parameter);      
  20.         if(rowCount>0){      
  21.             System.out.println("创建表成功");      
  22.         }      
  23.     }      
  24.           
  25.     //插入数据      
  26.     private void createNewUser(int age,String name){      
  27.         sqlString = "INSERT INTO t_user VALUES(?,?,?);";      
  28.         v_parameter.clear();      
  29.         v_parameter.add(null);      
  30.         v_parameter.add(age);      
  31.         v_parameter.add(name);      
  32.         rowCount = connDB.doUpdate(sqlString,v_parameter);      
  33.         if(rowCount>0){      
  34.             System.out.println("创建用户成功");      
  35.         }      
  36.     }      
  37.           
  38.     //查询数据      
  39.     private int selectUser(String name)throws Exception{      
  40.         sqlString = "SELECT * FROM t_user WHERE c_userName=?;";      
  41.         v_parameter.clear();      
  42.         v_parameter.add(name);      
  43.         ResultSet rs = connDB.doQuery(sqlString,v_parameter);      
  44.         rs.next();      
  45.         return Integer.parseInt(rs.getString("i_Id"));      
  46.     }      
  47.           
  48.     //删除数据      
  49.     private void deleteUser(String name)throws Exception{      
  50.         int id = selectUser(name);      
  51.         sqlString = "DELETE FROM t_user WHERE i_Id=?;";      
  52.         v_parameter.clear();      
  53.         v_parameter.add(id);      
  54.         rowCount = connDB.doUpdate(sqlString,v_parameter);      
  55.         if(rowCount>0){      
  56.             System.out.println("删除成功");      
  57.         }      
  58.     }      
  59.           
  60.     //更新数据      
  61.     private void updataUser(int id,int age,String name){      
  62.         sqlString = "UPDATE t_user SET i_age=?,c_userName=? WHERE i_Id=?";      
  63.         v_parameter.clear();      
  64.         v_parameter.add(age);      
  65.         v_parameter.add(name);      
  66.         v_parameter.add(id);      
  67.         rowCount = connDB.doUpdate(sqlString,v_parameter);      
  68.         if(rowCount>0){      
  69.             System.out.println("更新成功");      
  70.         }      
  71.     }      
  72.           
  73.     public static void main(String[] args){      
  74.         try{      
  75.             ConnTest test = new ConnTest();      
  76.             test.createTable();      
  77.             test.createNewUser(18,"杨sheep冠");      
  78.             int id = test.selectUser("杨sheep冠");      
  79.             test.updataUser(id,23,"杨sheep");      
  80.             test.deleteUser("杨sheep");      
  81.             test.createNewUser(22,"yang");      
  82.         }      
  83.         catch(Exception e){      
  84.             e.printStackTrace();      
  85.         }      
  86.     }      
  87. }    

本文是个人原创,如文中有错或你有建议,请留言指出,如要交流请加QQ519870018,如要转载本文,请标明本文出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值