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

JDBC连接mysql

时间:2017-07-21 01:14:29      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:mysq   catch   127.0.0.1   show   驱动器   created   ring   test   cut   

/**
* Created by admin on 2017/1/28.
*/
import java.sql.*;

public class MySQL {
public static void main(String[] args){
// jdbc 驱动器, 需要自己下载驱动器,否则无法连接到数据库
String driver = "com.mysql.jdbc.Driver";
// 连接地址
String url = "jdbc:mysql://127.0.0.1:3306/china";
String user = "root";
String password = "admin";
try{
// 加载mysql 驱动
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if (!conn.isClosed()){
System.out.println("connect successfully!");
}
Statement statement = conn.createStatement();
String sql = "show databases;";
ResultSet res = statement.executeQuery(sql);
// String data = null;
while (res.next()){
System.out.println(res.first());
}
}
catch (ClassNotFoundException e){
System.out.println("connect fail can‘t not found driver");
e.printStackTrace();
}
catch (SQLException e){
e.printStackTrace();

}
}
}

JDBC连接mysql

标签:mysq   catch   127.0.0.1   show   驱动器   created   ring   test   cut   

原文地址:http://www.cnblogs.com/SunshineLittleCat/p/7215375.html

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