I'm a newbie in programming (Java) and I'm trying to figure out how to work with databases, using Microsoft SQL Server 2012.
I got the code below from a book, with changes only to the local server address, password, database name, etc.
The code runs indefinitely and only prints "The driver has been loaded!"
Also, it seems to make no difference what password or user name I write. I've tried many different address formats that I got from Microsoft and forums. Can anyone please tell me what I'm doing wrong here? Thanks!
package CoisaIdiota;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TesteDB {
static String ConnectURL = "jdbc:sqlserver://localhost:1433;databaseName=teste";
static String user = "Adm-PC\\Adm";
static String pw = "password";
static Connection conn;
public static void main(String[] args) {
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
} catch (Exception e) {
System.err.println("The driver couldn’t be loaded!");
System.err.println(e);
e.printStackTrace();
System.exit(-1);
}
System.out.println("The driver has been loaded!");
try {
conn = DriverManager.getConnection(ConnectURL, user, pw);
Statement stmt = conn.createStatement();
String query = "select ID from nomes";
ResultSet rs = stmt.executeQuery(query);
while(rs.next()){
System.out.println(rs.getString("ID"));
}
rs.close();
stmt.close();
} catch (SQLException e){
System.err.println("No connection possible.");
e.printStackTrace();
System.err.println("SQLException: " + e.getMessage());
System.err.println("SQLState: " + e.getSQLState());
System.err.println("VendorError: " + e.getErrorCode());
System.exit(-1);
}
}
}