eclipse怎么连接mysql,而且mysql版本高
2016-02-03 · 百度知道合伙人官方认证企业
eclipse连接mysql需要导入mysql的驱动包,更版本无关。
配置之前请先下载mysql-connector-java-5.1.15-bin.jar文件。
右键单击包所在的工程包(project),Build Path ---> Configure Build Path,在弹出的窗口中选择 Add External JARs。把下载并解压出来的mysql-connector-java-5.1.15-bin.jar选中。如图:
配置下载好的驱动jar:
编写连接程序:
import java.sql.*;
class ConnMySql {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/select_test",
"root","123456");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from teacher_table");
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t"
+rs.getString(2) + "\t"
+rs.getString(3) );
}
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}
{
try{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("数据库驱动加载成功!");
String url="jdbc:mysql://localhost:3306/dbname"; //dbname改为你数据库的实际名字
Stirng user="root"; //root改成你实际的数据库登录名
String passWord="123"; //123 改为实际的密码
conn=DriverManager.getConnection(url,user,passWord);
System.out.println("mysql数据库已连接成功!");
}catch(Exception e){
e.printStackTrace();
}
}
别忘了把mysql的jdbc包加进去
2015-12-19 · 做真实的自己 用良心做教育
数据库名:select_test
用户名:root
密码:123456
连接成功后显示teacher_table表中的数据。
import java.sql.*;
class ConnMySql {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306lect_test",
"root","123456");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from teacher_table");
while (rs.next()) {
System.out.println(rs.getInt(1) + "\t"
+rs.getString(2) + "\t"
+rs.getString(3) );
}
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
}