java串連mysql資料庫增刪改查操作記錄,javamysql

來源:互聯網
上載者:User

java串連mysql資料庫增刪改查操作記錄,javamysql

1. 串連資料庫,得到資料庫連接變數

注意串連資料庫的時候

(1)開啟DB Browser 建立一個Database Driver,注意添加Driver JARs的時候添加的包,我的是mysql-connector-java-5.0.3-bin.jar

(2)要將資料庫jar包拷貝到工程下的WEB-INF\lib下

    import java.sql.Connection;//java包      public class DBConnection          {              private String dbDriver="com.mysql.jdbc.Driver";               private String dbUrl="jdbc:mysql://[ip地址]:[連接埠號碼]/[資料庫名]";//根據實際情況變化              private String dbUser="root";              private String dbPass="root";              public Connection getConn()              {                  Connection conn=null;                  try                  {                      Class.forName(dbDriver);                  }                  catch (ClassNotFoundException e)                  {                      e.printStackTrace();                  }                  try                  {                      conn = DriverManager.getConnection(dbUrl,dbUser,dbPass);//注意是三個參數                  }                  catch (SQLException e)                  {                      e.printStackTrace();                  }                  return conn;              }          }  

2. 插入操作

    public int insert()      {          int i=0;          String sql="insert into (表名)(列名1,列明2) values(?,?)";          Connection cnn=getConn();                try{              PreparedStatement preStmt =cnn.prepareStement(sql);              preStmt.setString(1,值);              preStmt.setString(2,值);//或者:preStmt.setInt(1,值);              i=preStmt.executeUpdate();          }          catch (SQLException e)          {              e.printStackTrace();          }          return i;//返回影響的行數,1為執行成功      }  

3. 更新操作

    public int update      {          int i=0;          String sql="update (表名) set  (列名1)=?,列明2=? where (列名)=?";//注意要有where條件          Connection cnn=getConn();                      try{              PreparedStatement preStmt =cnn.prepareStatement(sql);              preStmt.setString(1,(值));              preStmt.setString(2,(值));//或者:preStmt.setInt(1,值);              preStmt.setInt(3,(值));              i=preStmt.executeUpdate();          }          catch (SQLException e)          {              e.printStackTrace();          }          return i;//返回影響的行數,1為執行成功      }  

4. 尋找操作
    public String select          {              String sql = "select * from (表名) where (列名)=(值)";              Connection cnn = getConn();//此處為通過自己寫的方法getConn()獲得串連              try              {                  Statement stmt = conn.createStatement();                  ResultSet rs = stmt.executeQuery(sql);                        if(rs.next())                  {                      int m1 = rs.getInt(1);//或者為rs.getString(1),根據資料庫中列的實值型別確定,參數為第一列                      String m2 = rs.getString(2);                  }                  //可以將尋找到的值寫入類,然後返回相應的對象              }              catch (SQLException e)              {                  e.printStackTrace();              }              return (相應的值的變數);          }  

5. 刪除操作

    public int delete()      {          String sql = "delete from (表名) where (列名)=(值)";          int i=0;          Connection conn = getConn();//此處為通過自己寫的方法getConn()獲得串連          try          {              Statement stmt = conn.createStatement();              i = stmt.executeUpdate(sql);          }          catch (SQLException e)          {              e.printStackTrace();          }          return i;//如果返回的是1,則執行成功;      }  


相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.