Problem conecting to SQL Server 2008
Hi, I'm trying to conect to a database in SQL Server 2008 but I'm having some problems. Here is my code and the error I got.
import java.sql.*;
public class Main {
public static void main(String args[]) {
System.out.println("Testing access to SQLServer\n\n");
Connection conn = null;
String teste = "SELECT Name, Adress FROM TB_Cliente;";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc: odbc: DSK_RENATO", "sa", "mope2008");
System.out.println("successl\n");
Statement stm = conn.createStatement();
ResultSet rs = stm.executeQuery(test);
while (rs.next()) {
String Nome = rs.getString("Name");
String Endereco = rs.getString("Adress");
System.out.println("Name: " +Name+ " Adress: " + Adress);
System.out.println("---------------------------------------");
}
System.out.println("\n\nSucess!!!\n");
} catch(ClassNotFoundException e) {
System.out.println("excessão Class not found");
e.printStackTrace();
} catch(SQLException e) {
System.out.println("SQL Exception... Error in querry:");
e.printStackTrace();
} finally {
try {
conn.close();
System.out.println("\n\nClosing the conection");
} catch(SQLException erro) {
System.out.println("Error in closing");
erro.printStackTrace();
}
}
}
}
Testing access to SQLServer
SQL Exception... Erro na consulta:
java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source names not found and none standart driver specified
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize(Unknow n Source)
at sun.jdbc.odbc.JdbcOdbcDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at Main.main(Main.java:11)
Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:30)
Thanks
problem connecting to sql server 2008
according to your error "java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source names not found and none standart driver specified"
it means that you have not yet created a datasource for your database connection. if you are using XP, u can create a datasource by going to
control panel -> administrative tools -> datasources . create a new datasource under the user dsn named dsk_renato, using sql server as your datasource, fill in the other requirements according to ur sql server details and rerun ur java code