利用mysql数据库,用时导入mysql-connector-java-5.1.18-bin.jar
import java.sql.*;
//通过Class.forname()得到
public class JdbcConnection {
//定义MySql的数据库驱动程序
public static final String DBDRIVER ="org.gjt.mm.mysql.Driver";
//定义mysql数据库连接
public static final String DBURL ="jdbc:mysql://localhost:3306/test";
//mysql数据库连接用户名密码
public static final String DBUSE ="root";
public static final String DBPASS ="root";
public static void main(String args[]){
//完整的包名称,包.类
Connection conn =null;
try {
Class.forName(DBDRIVER);
System.out.print("welcome!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try{
conn=DriverManager.getConnection(DBURL,DBUSE,DBPASS);
}catch(SQLException e){
e.printStackTrace();
}
System.out.println(conn);
try {
conn.close();//关闭数据库,一定要记住
} catch (SQLException e) {
e.printStackTrace();
}
}
}