Lenquist

Connecting to SQL Server 2008 with JDBC

May 27th, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.83 KB | None | 0 0
  1. /*source: http://stackoverflow.com/questions/2451892/how-do-i-connect-to-a-sql-server-2008-database-in-java-with-jdbc*/
  2.  
  3. import java.sql.*;
  4. import javax.sql.*;
  5.  
  6. public class jdbcdemo{
  7.  
  8. public static void main(String args[]){
  9. String dbtime;
  10. String dbUrl = "jdbc:mysql://your.database.domain/yourDBname";
  11. String dbClass = "com.mysql.jdbc.Driver";
  12. String query = "Select * FROM users";
  13.  
  14. try {
  15.  
  16. Class.forName("com.mysql.jdbc.Driver");
  17. Connection con = DriverManager.getConnection (dbUrl);
  18. Statement stmt = con.createStatement();
  19. ResultSet rs = stmt.executeQuery(query);
  20.  
  21. while (rs.next()) {
  22. dbtime = rs.getString(1);
  23. System.out.println(dbtime);
  24. } //end while
  25.  
  26. con.close();
  27. } //end try
  28.  
  29. catch(ClassNotFoundException e) {
  30. e.printStackTrace();
  31. }
  32.  
  33. catch(SQLException e) {
  34. e.printStackTrace();
  35. }
  36.  
  37. }  //end main
  38.  
  39. }  //end class
Advertisement
Add Comment
Please, Sign In to add comment