main code:
//驱动程序名
String driver = "com.mysql.jdbc.Driver";
//URL指向要访问的数据库
String url = "jdbc:mysql://127.0.0.1:3306/dateBase";
//Mysql配置时的用户名
String user = "root";
//Mysql配置时的密码
String password = "1234";
try{
//加载驱动程序
Class.forName(driver);
//连接数据库
Connection conn = DriverManage.getConnection(url, user, password);
if(!conn.isClosed)
{
System.out.println("Succeeded connecting to the DataBase!");
}
//statement 用来执行SQL语句
Statement statement = conn.creatStatement();
//要执行的sql语句
String sql = "select * from student";
//结果集
ResultSet rs = statement.executeQuery(sql);
String name = null;
while(rs.next()){
name = rs.getString("sname");
name = new String(name.getBytes("ISO-8859-1"),"GB2312");
system.out.println(re.getString("sno")+"\t"+name);
}
rs.close();
conn.close();