Java连接MySQL数据库操作

package JavaDatabaseProgramming;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

//连接MySQL数据库操作
public class LianJieShuJuKu {

//定义MySQL的数据库驱动程序
public static final String DBDRIVER="org.gjt.mm.mysql.Driver";
//定义MySQL数据库的链接地址,3306为mysql数据库的端口号,user为数据库名称
public static final String DBURL="jdbc:mysql://localhost:3306/user";
//MySQL数据库的连接用户名,root为MySQL默认超级管理员的登录名
public static final String DBUSER="root";
//MySQL数据库的连接密码,1为链接数据库的密码
public static final String DBPASS="1";

public static void main(String[] args) {
Connection conn = null; //数据库连接
try{
Class.forName(DBDRIVER); //加载数据库驱动程序
}catch(ClassNotFoundException e){
e.printStackTrace();
}
try{
//连接MySQL数据库时,需要写上连接的用户名和密码
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
}catch(SQLException e){
e.printStackTrace();
}
System.out.println(conn);
try{
conn.close(); //关闭数据库
}catch(SQLException e){
e.printStackTrace();
}
}

}

posted @ 2015-04-22 15:18  宋发元  阅读(149)  评论(0)    收藏  举报