码迷,mamicode.com
首页 > 数据库 > 详细

java通过jdbc链接数据库

时间:2015-01-21 16:32:31      阅读:205      评论:0      收藏:0      [点我收藏+]

标签: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链接数据库

标签:java   数据库   jdbc   mysql   

原文地址:http://blog.csdn.net/puqutogether/article/details/42969965

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!