1. MySQL JDBC驱动下载
2.导入MySQLJDBC驱动
Eclipse: Build Path->Libraries->Add External JARs…->mysql-connector-java-5.0.8-bin.jar
3. 建立MySQL连接:
publicvoid conMySQL() {
Stringmydriver="com.mysql.jdbc.Driver";
Stringmyurl="jdbc:mysql://10.224.xx.xx:3306/mysql";
Stringmyuser="test";
Stringmypassword="pass";
try{
Class.forName(mydriver);
long s=System.currentTimeMillis();
System.out.println("MySQL Connectstart.");
con = DriverManager.getConnection(myurl,myuser,mypassword);
st=con.createStatement();
/*rs=st.executeQuery("select* from user");
try{
while(rs.next()){
System.out.println(rs.getString(1)+""+rs.getString(2));
}
}catch(Exception e){
System.out.println("Wrongerror.");
e.printStackTrace();
}*/
System.out.println("MySQL Connectsuccessfully.");
long e=System.currentTimeMillis();
System.out.println("TimeEclipsed: "+(e-s));
if(!con.isClosed()){
System.out.println("Succeededconnecting to the Database!");
}
}catch (Exception e){
System.out.println("Not foundMySQL");
e.printStackTrace();
}
}