Java 通过JDBC连接Mysql数据库的方法和实例

Java是通过JDBC连接Mysql数据库的。JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序

如果要使用数据库就要添加数据库的驱动,不同的数据库有不用的驱动,这里就不一一说明,添加jar程序驱动包的方法就不在这里解释,

另一个文章里面有介绍http://blog.csdn.NET/huluedeai/article/details/50414400


下面是一个实例去介绍mysql数据库的连接,其它数据库的方法也是差不多的。

[java]  view plain  copy
  1. import java.sql.*;  
  2. public class MysqlTest {  
  3.     public static void main(String[] args){  
  4.                // 驱动程序名        
  5.         String driver = "com.mysql.jdbc.Driver";  
  6.                // URL指向要访问的数据库名world        
  7.         String url = "jdbc:mysql://127.0.0.1:3306/world";  
  8.                // MySQL配置时的用户名           
  9.         String user = "root";           
  10.         // MySQL配置时的密码          
  11.         String password = "123456";  
  12.         String name;  
  13.                 try {               
  14.                  // 加载驱动程序        
  15.                 Class.forName(driver);  
  16.                     // 连续数据库       
  17.                Connection conn = DriverManager.getConnection(url, user, password);  
  18.                    if(!conn.isClosed())          
  19.                   System.out.println("Succeeded connecting to the Database!");  
  20.                   // statement用来执行SQL语句             
  21.                      Statement statement = conn.createStatement();  
  22.                  // 要执行的SQL语句           
  23.                    String sql = "select * from city";  
  24.                 // 结果集       
  25.                   ResultSet rs = statement.executeQuery(sql);  
  26.                 while(rs.next())  {         
  27.                // 选择Name这列数据     
  28.                name = rs.getString("Name");  
  29.                   // 输出结果              
  30.                   System.out.println(rs.getString("CountryCode") + "\t" + name);           
  31.              }  
  32.          rs.close();       conn.close();  }   
  33.         catch(ClassNotFoundException e) {  
  34.          System.out.println("Sorry,can`t find the Driver!");              
  35.          e.printStackTrace();  
  36.         } catch(SQLException e) {  
  37.          e.printStackTrace();  
  38.         } catch(Exception e) {  
  39.          e.printStackTrace();  
  40.         }   
  41.         }  
  42. }  

Note:如果你没有添加jar程序驱动包的话编译的时候发现以下问题:

Sorry,can`t find the Driver!

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

    atjava.Net.URLClassLoader$1.run(Unknown Source)

    atjava.net.URLClassLoader$1.run(Unknown Source)

    atjava.security.AccessController.doPrivileged(Native Method)

    atjava.net.URLClassLoader.findClass(Unknown Source)

    atjava.lang.ClassLoader.loadClass(Unknown Source)

    atsun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

    atjava.lang.ClassLoader.loadClass(Unknown Source)

    atjava.lang.Class.forName0(Native Method)

     atjava.lang.Class.forName(Unknown Source)


这时,你只需要按照http://blog.csdn.net/huluedeai/article/details/50414400里的方法就可以解决。


但是编译运行虽然成功,但是有以下的warning信息:

Thu Dec 24 00:08:37 CST 2015WARN: Establishing SSL connection without server's identity verification is notrecommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSLconnection must be established by default if explicit option isn't set. Forcompliance with existing applications not using SSL the verifyServerCertificateproperty is set to 'false'. You need either to explicitly disable SSL bysetting useSSL=false, or set useSSL=true and provide truststore for servercertificate verification.

Succeeded connecting to theDatabase!

AFG Kabul

AFG Qandahar

AFG Herat

AFG Mazar-e-Sharif

NLD Amsterdam

NLD Rotterdam

NLD Haag

这时只需要把url字符串改为以下:

改之前:String url ="jdbc:mysql://127.0.0.1:3306/world";

改之后:String url = "jdbc:mysql://127.0.0.1:3306/world?useUnicode=true&characterEncoding=utf-8&useSSL=false";


就发现warning没了,程序运行成功。

Succeeded connecting to theDatabase!

AFG Kabul

AFG Qandahar

AFG Herat

AFG Mazar-e-Sharif

NLD Amsterdam

NLD Rotterdam

NLD Haag

NLD                                 Utrecht


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值