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 + "");
}
}
}