首先绑上Jar包,安装配置好MtySql后,就可以测试了
MySql在Vista下的安装有点麻烦... 这以后再说
- import java.sql.*;
- public class MyMqlConnection {
- /**
- *
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String driveName = "com.mysql.jdbc.Driver";
- String databaseURL = "jdbc:mysql://localhost:3306/test";
- String user = "root";
- String password = "mysql";// 此处写自己的密码
- try {
- Class.forName(driveName);
- System.out.println("成功加载数据库驱动程序");
- }
- catch (java.lang.ClassNotFoundException e) {
- System.out.println("加载数据库驱动程序失败");
- System.out.println(e.getMessage());
- return;
- }
- try {
- Connection con = DriverManager.getConnection(databaseURL, user,
- password);
- System.out.println("连接数据库成功");
- con.close();
- }
- catch (SQLException e) {
- System.out.println("连接数据库失败");
- System.out.println("SQLException:" + e.getMessage());
- return;
- }
- }
- }