请教一下Java连接mysql数据库问题:eclipse报错:Unknown database 'student'
我用的是jdk和jdbc、eclipse、mysel数据库,创建的数据库名:test,表名:student;表内容:-------------------snosnme-...
我用的是jdk和jdbc、eclipse、mysel数据库,创建的数据库名:test,表名:student;表内容:
-------------------
sno snme
-------------------
1 小孩
2 猴子
3 敌法
------------------
下面是eclipse的Java代码:
import java.sql.*;
public class DbConnect {
public static void main(String[] args){
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.0.48:3306/student";
String user = "root";
String password = "root";
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeed");
Statement statement = conn.createStatement();
String sql = "select * from test.student";
ResultSet rs = statement.executeQuery(sql);
System.out.println("----------");
System.out.println("执行结果如下所示:");
System.out.println("----------");
System.out.println("学好" + "\t" + "姓名");
System.out.println("----------");
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") + "\t" + name);
}
rs.close();
conn.close();
}
catch(ClassNotFoundException e){
System.out.println("sorry");
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
} 展开
-------------------
sno snme
-------------------
1 小孩
2 猴子
3 敌法
------------------
下面是eclipse的Java代码:
import java.sql.*;
public class DbConnect {
public static void main(String[] args){
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://192.168.0.48:3306/student";
String user = "root";
String password = "root";
try{
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeed");
Statement statement = conn.createStatement();
String sql = "select * from test.student";
ResultSet rs = statement.executeQuery(sql);
System.out.println("----------");
System.out.println("执行结果如下所示:");
System.out.println("----------");
System.out.println("学好" + "\t" + "姓名");
System.out.println("----------");
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") + "\t" + name);
}
rs.close();
conn.close();
}
catch(ClassNotFoundException e){
System.out.println("sorry");
e.printStackTrace();
}
catch(SQLException e){
e.printStackTrace();
}
catch(Exception e){
e.printStackTrace();
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询