• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 429
  • Last Modified:

JDBC and MS SQL

this question may fit better into the Database category instead of Java. Anyway, I hope somebody can help me here.

I have successfully connected to MS SQl server via JDBC-ODBC bridge driver.  My problem is I dont know how to access the table, I created a table names emp under "tempdb"(or something like that), but when I tried to the statement "select * from emp", I was told "invalid object name emp".   I never used MS SQL before, I have no idea about it.  is there command line inferface like SQL * plus for Oracle in MS SQL?  what is wrong with the emp table I created?

Thanks & merry x-mas
0
Embt
Asked:
Embt
  • 3
1 Solution
 
zhongbingCommented:
With SQL Server Entreprise Manage, You can browse the database, table , etc.
0
 
zhongbingCommented:
 And don't create table under tempdb , which is a reserved database used by SQl.

**************from MSDN*************
tempdb
tempdb holds all temporary tables and temporary stored procedures. It also fills any other temporary storage needs such as work tables generated by SQL Server. tempdb is a global resource; the temporary tables and stored procedures for all users connected to the system are stored there. tempdb is re-created every time SQL Server is started so the system starts with a clean copy of the database. Because temporary tables and stored procedures are dropped automatically on disconnect, and no connections are active when the system is shut down, there is never anything in tempdb to be saved from one session of SQL Server to another.

tempdb autogrows as needed. Each time the system is started, tempdb is reset to its default size. You can avoid the overhead of having tempdb autogrow by using ALTER DATABASE to increase the size of tempdb.
**********************************
0
 
EmbtAuthor Commented:
here is my code:
import java.sql.*;

public class Jdbc_odbc {

public static void main(String args[]) throws SQLException {

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}

Connection conn = DriverManager.getConnection("jdbc:odbc:test", "", "");

System.out.println("Connected!");

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from emp");
while(rs.next()) {
   int  empno = rs.getInt(1);
   String  ename = rs.getString(2);
  System.out.println(empno + "\t" + ename);
}

rs.close();
conn.close();

}
}

in MS SQL server, under which catogory can I create the table so I can access it from the query in my java code?
0
 
prasadkkCommented:
By default the ODBC for SQL Server searches only in the master db. While configuring the DSN for SQL Server, you have to specify the database as tempdb.
0
 
zhongbingCommented:
   create your new database and create table inside it. And make sure your dsn is correctly configured.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now