JSF JDBC connection to SQL server 2005
I have fianlly got around to figuring out how to connect to a sql server 2005 using a JDBC in JSF. Here is how my code look:
Connection connection = DriverManager.getConnection(“jdbc:sqlserver://localhost;databaseName=dbName1;user=user;password=password”);
will register the JDBC driver that I downloaded from Microsoft from the site:
so that your DriverManager class can use it for the connection.
The rest of the code is pretty self explanitory. There is plenty of doco that comes with the driver itself.
But How do I use it?
good question, I am using a class called DatabaseDelegate which will be used to connect to the database. Then once I have an instance of the DatabaseDelegate (and hence a connection to the DB) I can use the following code for execution:
DatabaseDelegate dd = new DatabaseDelegate();
conn = dd.getConnection();
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(“select id, name from users”);
//do stuff as needed with the results
Hope this is clear enough for any newbies to JDBC’s