Java连接mysql数据库常用功能框架

来源:互联网 发布:js分割字符串 编辑:程序博客网 时间:2024/05/16 07:36

代码


  1. import java.sql.*;    
  2. class myConnection{  
  3.     private String dbDriver="com.mysql.jdbc.Driver";  
  4.     private String dbUrl="jdbc:mysql://id:3306/test";//根据实际情况变化  
  5.     private String dbUser="root";  
  6.     private String dbPass="123456";  
  7.     public java.sql.Connection getConn()  
  8.     {  
  9.         java.sql.Connection conn=null;  
  10.         try  
  11.         {  
  12.             Class.forName(dbDriver);  
  13.         }  
  14.         catch (ClassNotFoundException e)  
  15.         {  
  16.             e.printStackTrace();  
  17.         }  
  18.         try  
  19.         {  
  20.             conn = DriverManager.getConnection(dbUrl, dbUser, dbPass);//注意是三个参数  
  21.             System.out.print("mysqlConnection.");  
  22.         }  
  23.         catch (SQLException e)  
  24.         {  
  25.             e.printStackTrace();  
  26.         }  
  27.         return conn;  
  28.     }  
  29.   
  30.     public int insert()  
  31.     {  
  32.         int i=0;  
  33.         String sql="insert into (表名)(列名1,列明2) values(?,?)";  
  34.         Connection cnn=getConn();  
  35.   
  36.         try{  
  37.             PreparedStatement preStmt =cnn.prepareStement(sql);  
  38.             preStmt.setString(1,值);  
  39.             preStmt.setString(2,值);//或者:preStmt.setInt(1,值);  
  40.             i=preStmt.executeUpdate();  
  41.         }  
  42.         catch (SQLException e)  
  43.         {  
  44.             e.printStackTrace();  
  45.         }  
  46.         return i;//返回影响的行数,1为执行成功  
  47.     }  
  48.     public int update  
  49.     {  
  50.         int i=0;  
  51.         String sql="update (表名) set  (列名1)=?,列明2=? where (列名)=?";//注意要有where条件  
  52.         Connection cnn=getConn();  
  53.   
  54.   
  55.         try{  
  56.             PreparedStatement preStmt =cnn.prepareStatement(sql);  
  57.             preStmt.setString(1,(值));  
  58.             preStmt.setString(2,(值));//或者:preStmt.setInt(1,值);  
  59.             preStmt.setInt(3,(值));  
  60.             i=preStmt.executeUpdate();  
  61.         }  
  62.         catch (SQLException e)  
  63.         {  
  64.             e.printStackTrace();  
  65.         }  
  66.         return i;//返回影响的行数,1为执行成功  
  67.     }  
  68.   
  69.     public String select  
  70.     {  
  71.         String sql = "select * from (表名) where (列名)=(值)";  
  72.         Connection cnn = getConn();//此处为通过自己写的方法getConn()获得连接  
  73.         try  
  74.         {  
  75.             Statement stmt = conn.createStatement();  
  76.             ResultSet rs = stmt.executeQuery(sql);  
  77.   
  78.             if(rs.next())  
  79.             {  
  80.                 int m1 = rs.getInt(1);//或者为rs.getString(1),根据数据库中列的值类型确定,参数为第一列  
  81.                 String m2 = rs.getString(2);  
  82.             }  
  83.             //可以将查找到的值写入类,然后返回相应的对象  
  84.         }  
  85.         catch (SQLException e)  
  86.         {  
  87.             e.printStackTrace();  
  88.         }  
  89.         return (相应的值的变量);  
  90.     }  
  91.   
  92.     public int delete()  
  93.     {  
  94.         String sql = "delete from (表名) where (列名)=(值)";  
  95.         int i=0;  
  96.         Connection conn = getConn();//此处为通过自己写的方法getConn()获得连接  
  97.         try  
  98.         {  
  99.             Statement stmt = conn.createStatement();  
  100.             i = stmt.executeUpdate(sql);  
  101.         }  
  102.         catch (SQLException e)  
  103.         {  
  104.             e.printStackTrace();  
  105.         }  
  106.         return i;//如果返回的是1,则执行成功;  
  107.     }  
  108. }  
  109. public class main {  
  110.   
  111.     public static void main(String[] args){  
  112.         myConnection con1 = new myConnection();  
  113.         con1.getConn();  
  114.     }  
  115. }  

原创粉丝点击