jdbc串連mysql

來源:互聯網
上載者:User

標籤:

要匯入相應的包]

 1 public class ConnDB { 2     private Connection ct = null; 3    //驅動程式名 4     String driver = "com.mysql.jdbc.Driver"; 5     // URL指向要訪問的資料庫名scutcs 6     String url = "jdbc:mysql://127.0.0.1:3306/develop"; 7     // MySQL配置時的使用者名稱 8     String user = "root"; 9     // Java串連MySQL配置時的密碼

10 String password = "wzy123";
11 12     public Connection getConn(){13         try{14             Class.forName(driver);    15             ct = DriverManager.getConnection(url, user, password);16         }17         catch(Exception e){18             e.printStackTrace();19         }20         return ct;21     }22 }

 

public int add(String day,String time,String volname,String volclass,String voltel,String volemail){Connection con = DbConnect.this.getConn();int num = -1;try {java.sql.Statement statement = con.createStatement(); num = statement.executeUpdate( "insert into tb_volinfo(day,time,volname,volclass,voltel,volemail) "+ "values(‘"+day+"‘,‘"+time+"‘,‘"+volname+"‘,‘"+volclass+"‘,‘"+voltel+"‘,‘"+volemail+"‘)");if(statement!=null){statement.close();}if(con!=null){con.close();//關閉資料庫連接}} catch (Exception e) {e.printStackTrace();}return num;}public boolean SelectVal(String volname,String voltel){Connection con1 = DbConnect.this.getConn();ResultSet result =null;try {java.sql.Statement statement = con1.createStatement();    result = statement.executeQuery("select * from tb_volinfo where volname=‘"+volname+"‘ and voltel=‘"+voltel+"‘;");while(result.next()){return true;}    if(statement!=null){statement.close();}if(con1!=null){con1.close();//關閉資料庫連接}} catch (Exception e) {e.printStackTrace();}return false;}

  

jdbc串連mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.