java通過jdbc連結資料庫

bigface1234fdfg發表於2015-01-21

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中連結資料庫了。




相關文章