一、安装mysql驱动: http://mvnrepository.com/search?q=mysql-conn
二、看数据库中有什么表安装:navicat for MySQL
文件类写数据库查询:
package hunteronHC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class baseServer {
// 连接MySql数据库,用户名和密码都是root
public static final String url = "jdbc:mysql://192.168.50.42:3306/jobhunter_test";
public static final String username = "test";
public static final String password = "test";
public static void main(String[] args) {
try {
// 1.加载MySql的驱动类
Class.forName("com.mysql.jdbc.Driver");
// 2.获取数据库链接
try {
Connection con = DriverManager.getConnection(url, username, password);
// 3.创建一个Statement
Statement stmt = con.createStatement();
String query = "select * from online ";
ResultSet re = stmt.executeQuery(query);
while (re.next()) {
System.out.println(re.getString("userId"));
}
} catch (SQLException se) {
System.out.println("数据库连接失败!");
se.printStackTrace();
}
} catch (ClassNotFoundException e) {
System.out.println("找不到驱动程序类 ,加载驱动失败!");
e.printStackTrace();
}
}
}