连接数据库SC,执行查询select * from student,数据库SC中存在表student,运行显示:
SQLException:对象名 'student' 无效import java.sql.*;
import java.sql.SQLException;
public class DataBaseTest {
public static Connection getConnection()throws SQLException, java.lang.ClassNotFoundException{
String url = "jdbc:sqlserver://localhost:1433;DatebaseName=SC";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String userName = "sa";
String password = "123456";
Connection con = DriverManager.getConnection(url, userName, password);
return con;
}
public static void main(String a[]){
try{
Connection con = getConnection();
Statement sql = con.createStatement();
String query ="select * from student";
ResultSet result = sql.executeQuery(query);
System.out.println("Student表中的数据如下:");
System.out.println("---------------------");
System.out.println(" 学号"+" 课程号"+" 成绩");
System.out.println("---------------------");
while(result.next()){
int number = result.getInt("s_no");
}
sql.close();
con.close();
}catch(java.lang.ClassNotFoundException e){
System.err.println("ClassNotFoundException:" );
System.err.println(e.getMessage());
}catch(SQLException ex){
System.err.println("SQLException:"+ex.getMessage());
}
}
}当改为select * from SC.dbo.student 后就可以了,请问这是为什么啊
难道DatebaseName=SC这句话没有作用第一次发帖,望高人指点
SQLException:对象名 'student' 无效import java.sql.*;
import java.sql.SQLException;
public class DataBaseTest {
public static Connection getConnection()throws SQLException, java.lang.ClassNotFoundException{
String url = "jdbc:sqlserver://localhost:1433;DatebaseName=SC";
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String userName = "sa";
String password = "123456";
Connection con = DriverManager.getConnection(url, userName, password);
return con;
}
public static void main(String a[]){
try{
Connection con = getConnection();
Statement sql = con.createStatement();
String query ="select * from student";
ResultSet result = sql.executeQuery(query);
System.out.println("Student表中的数据如下:");
System.out.println("---------------------");
System.out.println(" 学号"+" 课程号"+" 成绩");
System.out.println("---------------------");
while(result.next()){
int number = result.getInt("s_no");
}
sql.close();
con.close();
}catch(java.lang.ClassNotFoundException e){
System.err.println("ClassNotFoundException:" );
System.err.println(e.getMessage());
}catch(SQLException ex){
System.err.println("SQLException:"+ex.getMessage());
}
}
}当改为select * from SC.dbo.student 后就可以了,请问这是为什么啊
难道DatebaseName=SC这句话没有作用第一次发帖,望高人指点
系统找不到SC.student
SQLException:对象名 'dbo.student' 无效。
SQLException:对象名 'student' 无效
究竟databasename是不是要连接的数据库名字?
默认会是在dbo上面的
----------------------------------------------
欢迎加入 JAVA爱好者进阶总群(161734506) 共同学习,共同发展,共同进步!