Java是通过JDBC连接Mysql数据库的。JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序。
如果要使用数据库就要添加数据库的驱动,不同的数据库有不用的驱动,这里就不一一说明,添加jar程序驱动包的方法就不在这里解释,
另一个文章里面有介绍http://blog.csdn.NET/huluedeai/article/details/50414400
下面是一个实例去介绍mysql数据库的连接,其它数据库的方法也是差不多的。
- import java.sql.*;
- public class MysqlTest {
- public static void main(String[] args){
- // 驱动程序名
- String driver = "com.mysql.jdbc.Driver";
- // URL指向要访问的数据库名world
- String url = "jdbc:mysql://127.0.0.1:3306/world";
- // MySQL配置时的用户名
- String user = "root";
- // MySQL配置时的密码
- String password = "123456";
- String name;
- try {
- // 加载驱动程序
- Class.forName(driver);
- // 连续数据库
- Connection conn = DriverManager.getConnection(url, user, password);
- if(!conn.isClosed())
- System.out.println("Succeeded connecting to the Database!");
- // statement用来执行SQL语句
- Statement statement = conn.createStatement();
- // 要执行的SQL语句
- String sql = "select * from city";
- // 结果集
- ResultSet rs = statement.executeQuery(sql);
- while(rs.next()) {
- // 选择Name这列数据
- name = rs.getString("Name");
- // 输出结果
- System.out.println(rs.getString("CountryCode") + "\t" + name);
- }
- rs.close(); conn.close(); }
- catch(ClassNotFoundException e) {
- System.out.println("Sorry,can`t find the Driver!");
- e.printStackTrace();
- } catch(SQLException e) {
- e.printStackTrace();
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- }
import java.sql.*;
public class MysqlTest {
public static void main(String[] args){
// 驱动程序名
String driver = "com.mysql.jdbc.Driver";
// URL指向要访问的数据库名world
String url = "jdbc:mysql://127.0.0.1:3306/world";
// MySQL配置时的用户名
String user = "root";
// MySQL配置时的密码
String password = "123456";
String name;
try {
// 加载驱动程序
Class.forName(driver);
// 连续数据库
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
// statement用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql = "select * from city";
// 结果集
ResultSet rs = statement.executeQuery(sql);
while(rs.next()) {
// 选择Name这列数据
name = rs.getString("Name");
// 输出结果
System.out.println(rs.getString("CountryCode") + "\t" + name);
}
rs.close(); conn.close(); }
catch(ClassNotFoundException e) {
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
} catch(SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
}
}
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