Hi

The error I stated in the title appears when I try to run the following code in Eclipse. I have created the Table which is named as "Emp" in Microsoft SQL Server. I can't find the error I have done here, and I googled but nothing helped me.. Plz help

import java.sql.*;

public class WriteToDB{
	
	 public static Connection getConnection() throws Exception{
		  
		  Connection conn = null;
		  String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
		    
		  Class.forName(driver).newInstance();
		  conn = DriverManager.getConnection("jdbc:sqlserver://CSLK-CISVR\\CISVR:1433", "sa", "cambio1234");
		  System.out.println("Connected to the database");
		
		  return conn;
		  
	  }
	 
	  public static void main(String[] args) {
	 
		  Connection conn = null;
		  Statement stmt = null;
		  ResultSet rs = null;
		  
		  try {
		      conn = getConnection();
		      // prepare query
		      String query = "SELECT * FROM Emp";
		      // create a statement
		      stmt = conn.createStatement();
		      // execute query and return result as a ResultSet
		      rs = stmt.executeQuery(query);
		      // get the column names from the ResultSet
		    //  getColumnNames(rs);
		    } catch (Exception e) {
		      e.printStackTrace();
		      System.exit(1);
		    }
		    
		    try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		    System.out.println("Connection Closed");
	 
	  }
  public static void getColumnNames(ResultSet rs) throws SQLException {
	    if (rs == null) {
	      return;
	    }
	    ResultSetMetaData rsMetaData = rs.getMetaData();
	    int numberOfColumns = rsMetaData.getColumnCount();
	    // get the column names; column indexes start from 1
	    for (int i = 1; i < numberOfColumns + 1; i++) {
	      String columnName = rsMetaData.getColumnName(i);
	      // Get the name of the column's table name
	      String tableName = rsMetaData.getTableName(i);
	      System.out.println("column name=" + columnName + " table=" + tableName + "");
	    }
	  } 
}

Emp is probably a reserved word. Try surrounding the name in quotes or brackets (since this is an MS produced DB it might use the Access quoting process, hence the brackets).

I.E.

"SELECT * FROM \"Emp\""
// or
"SELECT * FROM [Emp]"

Thanx for the help. But it doesn't work and I tried more with your help and I could run it in the following way..

"SELECT * FROM [DataBaseName].[dbo].[Emp]"

Yep, that's the other thing, using the schema/owner references. I don't know that the brackets are necessary there, and even if they are, then only around the reserved words.

This question has already been answered. Start a new discussion instead.