java通過jdbc連結資料庫

來源:互聯網
上載者:User

標籤:java   資料庫   jdbc   mysql   

java通過jdbc連結資料庫

    1. 在資料庫中建立一個表;




敲入mysql語句,建表:注意primary key的設定。




吐槽一下,這個表的欄位真多。。。

然後我們回到java中,通過jdbc來連結這個資料表。


import java.sql.Connection;  //需要import這兩個jar包import java.sql.DriverManager;  public static Connection ConnectToDB(){Connection conn = null; String sql; int result=0; String url = "jdbc:mysql://192.168.1.134/crawl?useUnicode=true&characterEncoding=UTF-8";  //需要資料庫url  String username = "zyp" ;   String password = "zyp123" ;try{Class.forName("com.mysql.jdbc.Driver"); // 動態載入mysql驅動conn = DriverManager.getConnection(url, username, password); Statement stmt = conn.createStatement();sql = "delete from crawl"; stmt.executeUpdate(sql);             return conn; }catch (SQLException e){System.out.println("資料庫連接失敗!");            e.printStackTrace();        } catch (Exception e) {            e.printStackTrace();        } return conn; }public void imsertIntoDB(int questionIndex, String question, String questionCategory, int questionGlanceTimes, Timestamp askTime, String questionDetails, int hasAcceptedAnswer, String acceptedAnswer, int acceptedCommentTimes, int acceptedGoodTimes, int acceptedBadTimes, int acceptedResponserRank, int acceptedResponserRate, String acceptedResponserTeam, String otherAnswer, int otherCommentTimes, int otherGoodTimes, int otherBadTimes, int otherResponserRank, double scoreOfPeopleRank, double scoreOfNumWords, double scoreOfGoodTimes, double totalScore, Connection conn) throws SQLException{try{String sql; sql = "insert into crawl(QuestionIndex, Question, QuestionCategory, QuestionGlanceTimes, AskTime, QuestionDetails, HasAcceptedAnswer, AcceptedAnswer, AcceptedCommentTimes, AcceptedGoodTimes, AcceptedBadTimes, AcceptedResponserRank, AcceptedResponserRate, AcceptedResponserTeam, OtherAnswer, OtherCommentTimes, OtherGoodTimes, OtherBadTimes, OtherResponserRank, ScoreOfPeopleRank, ScoreOfNumWords, ScoreOfGoodTimes, TotalScore) "    + "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";  //欄位佔位PreparedStatement stmt = conn.prepareStatement(sql);stmt.setObject(1, questionIndex); //有多少個欄位,就要明確每個?的含義stmt.setObject(2, question);stmt.setObject(3, questionCategory);stmt.setObject(4, questionGlanceTimes);stmt.setObject(5, askTime);stmt.setObject(6, questionDetails); stmt.setObject(7, hasAcceptedAnswer);stmt.setObject(8, acceptedAnswer);stmt.setObject(9, acceptedCommentTimes);stmt.setObject(10, acceptedGoodTimes);stmt.setObject(11, acceptedBadTimes);stmt.setObject(12, acceptedResponserRank);stmt.setObject(13, acceptedResponserRate);stmt.setObject(14, acceptedResponserTeam);stmt.setObject(15, otherAnswer);stmt.setObject(16, otherCommentTimes);stmt.setObject(17, otherGoodTimes);stmt.setObject(18, otherBadTimes);stmt.setObject(19, otherResponserRank);stmt.setObject(20, scoreOfNumWords);stmt.setObject(21, scoreOfGoodTimes);stmt.setObject(22, scoreOfGoodTimes);stmt.setObject(23, totalScore);System.out.println(question); int result;     result = stmt.executeUpdate(); }catch(SQLException e){System.out.println("MySQL操作錯誤");e.printStackTrace();throw e; }}


  然後在主函數中先運行connectToDB,返回connection類型的conn,給insertIotoDB傳參數conn和其他欄位的值,即可。

    需要import資料庫的時候,直接每次都調用insertIotoDB函數即可。

    效果如下:




    這樣以後就會在java中連結資料庫了。




java通過jdbc連結資料庫

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.