1.project -> build path -> add external archives -> mysql-connector-java-5.1.49.jar
you can download this pakage from dev.mysql.com/downloads/w…
package jdbcProject;
import java.sql.*;
public class mySql {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("success loading MySql driver!");
} catch (Exception e) {
System.out.println("Error loading MySql Driver!");
e.printStackTrace();
}
try {
System.out.println("start connecting");
String url = "jdbc:mysql://127.0.0.1:3306/mySql?useUnicode=true&characterEncoding=utf-8&useSSL=false";
String login = "root";
String password = "******"; //enter your mysql password
Connection connect = DriverManager.getConnection(url, login, password);
System.out.println("Success connect Mysql server!");
Statement stmt = connect.createStatement();
String database = "***";//enter your database's name
String table = "***"; //enter your table's name
ResultSet rs = stmt.executeQuery("select * from " + database + "." + table);
//user 为你表的名称
while (rs.next())
{
ArrayList<String> arrayOfColons = new ArrayList<String>();
String[] nameOfColons = {"*", "**"}; //enter names of colons
for(String nameOfColon : nameOfColons) {
arrayOfColons.add(rs.getString(nameOfColon));
}
for(String colon : arrayOfColons) {
System.out.print(colon + "\t");
}
System.out.println();
}
rs.close();
connect.close(); // 关闭连接
} catch (Exception e) {
e.printStackTrace();
}
}
}