jdbc连接mysql

 

首先要下载Connector/J地址:http://www.mysql.com/downloads/connector/j/

这是MySQL官方提供的连接方式:

解压后得到jar库文件,需要在工程中导入该库文件

我是用的是Eclipse:

 

  

 

 

 JAVA连接MySQL稍微繁琐,所以先写一个类用来打开或关闭数据库:

DBHelper.java

 

 1 package com.hu.demo;  
 2   
 3 import java.sql.Connection;  
 4 import java.sql.DriverManager;  
 5 import java.sql.PreparedStatement;  
 6 import java.sql.SQLException;  
 7   
 8 public class DBHelper {  
 9     public static final String url = "jdbc:mysql://127.0.0.1/student";  
10     public static final String name = "com.mysql.jdbc.Driver";  
11     public static final String user = "root";  
12     public static final String password = "root";  
13   
14     public Connection conn = null;  
15     public PreparedStatement pst = null;  
16   
17     public DBHelper(String sql) {  
18         try {  
19             Class.forName(name);//指定连接类型  
20             conn = DriverManager.getConnection(url, user, password);//获取连接  
21             pst = conn.prepareStatement(sql);//准备执行语句  
22         } catch (Exception e) {  
23             e.printStackTrace();  
24         }  
25     }  
26   
27     public void close() {  
28         try {  
29             this.pst.close();  
30             this.conn.close();  
31         } catch (SQLException e) {  
32             e.printStackTrace();  
33         }  
34     }  
35 }  

 

再写一个Demo.java来执行相关查询操作

Demo.java

 1 package com.hu.demo;  
 2   
 3 import java.sql.ResultSet;  
 4 import java.sql.SQLException;  
 5   
 6 public class Demo {  
 7   
 8     static String sql = null;  
 9     static DBHelper db1 = null;  
10     static ResultSet ret = null;  
11   
12     public static void main(String[] args) {  
13         test1();  
14         test2();///执行带参数的sql语句
15     }
16 
17     private static void test1() {
18         sql = "select *from stuinfo";//SQL语句  
19         db1 = new DBHelper(sql);//创建DBHelper对象  
20   
21         try {  
22             ret = db1.pst.executeQuery();//执行语句,得到结果集  
23             while (ret.next()) {  
24                 ///根据顺序取出结果集
25                 String uid = ret.getString(1); 
26                 String ufname = ret.getString(2);  
27                 String ulname = ret.getString(3);  
28                 String udate = ret.getString(4);  
29                 System.out.println(uid + "\t" + ufname + "\t" + ulname + "\t" + udate );  
30             }//显示数据  
31             ret.close();  
32             db1.close();//关闭连接  
33         } catch (SQLException e) {  
34             e.printStackTrace();  
35         }
36     }  
37     ///带参数的sql语句
38     private static void test2() {
39         ///带参数的SQL语句 ---------
40         sql = "select *from stuinfo where chess_id = ? and name = ?"; 
41         db1 = new DBHelper(sql);//创建DBHelper对象  
42         
43         try {  
44             ///设置参数到sql语句中--------
45             db1.pst.setString(1, "123");
46             db1.pst.setString(2, "飞机");
47             
48             ret = db1.pst.executeQuery();//执行语句,得到结果集  
49             while (ret.next()) {
50                 ///根据列名取出结果值---------
51                 String uid = ret.getString("chess_id");  
52                 String ufname = ret.getString("name");  
53                 String ulname = ret.getString("data");  
54                 String udate = ret.getString("data2");  
55                 System.out.println(uid + "\t" + ufname + "\t" + ulname + "\t" + udate );  
56             }//显示数据  
57             ret.close(); ///关闭结果集的数据库的资源
58             db1.close();//关闭连接  
59         } catch (SQLException e) {  
60             e.printStackTrace();  
61         }  
62     }
63   
64 }  

 

posted @ 2017-10-20 11:18  戈博折刀  阅读(252)  评论(0编辑  收藏  举报