怎么用jdbc连接mysql数据库
2016-08-14 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
关注
展开全部
首先导入驱动jar包,程序是
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class connectionMySql {
private static Connection connection;
static{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/purview";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.print("加载驱动失败");
e.printStackTrace();
}
try {
connection=DriverManager.getConnection(url, "root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
return connection;
}
public static void main(String [] args) {
Connection con=connectionMySql.getConnection();
if(!con.equals("")){
System.out.print("数据库连接成功,连接id是:"+con);
}
}
}
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class connectionMySql {
private static Connection connection;
static{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/purview";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.print("加载驱动失败");
e.printStackTrace();
}
try {
connection=DriverManager.getConnection(url, "root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() {
return connection;
}
public static void main(String [] args) {
Connection con=connectionMySql.getConnection();
if(!con.equals("")){
System.out.print("数据库连接成功,连接id是:"+con);
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询