1 安装mysql数据库
2 添加mysql数据库的驱动
3 java测试代码为
/**
*
*/
import java.sql.*;
/**
* @author ueng
*
*/
public class connDb {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:MySQL://127.0.0.1:3306/test";
String user = "root";
String password = "*****";
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the database");
Statement stmt = conn.createStatement();
String sql = "select *from student";
ResultSet rs = stmt.executeQuery(sql);
String name = null;
while(rs.next()){
name = rs.getString("sname");
name = new String(name.getBytes("ISO-8859-1"),"GB2312");
System.out.println(rs.getString("sno")+" "+name);
}
rs.close();
conn.close();
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}