faqts : Computers : Programming : Languages : Java

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

12 of 28 people (43%) answered Yes
Recently 4 of 10 people (40%) answered Yes

Entry

how to connect to MSSQL database driver directly without ODBC , i have used url,pssw and username

Apr 5th, 2006 07:09
RAJESH WARRIER, denver dsouza,


this is not the complete code, but it will get you started.
note: change URL, username, password and table name as per your local
environment. You need to have MQSQL jdbc driver in your classpath.
  try{
     Class.forName( "com.mysql.jdbc.Driver" ) ;
      String url =
            "jdbc:mysql://localhost:3306/mysql"
      // Get a connection to the database
     Connection con =
                     DriverManager.getConnection(
                                 url,"username", "password");
     Statement stmt = conn.createStatement() ;
      // Execute the query
      ResultSet rs = stmt.executeQuery( "SELECT * FROM Cust" ) ;
      // Loop through the result set
      while( rs.next() )
         System.out.println( rs.getString(1) ) ;
      // Close the result set, statement and the connection
      rs.close() ;
      stmt.close() ;
      conn.close() ;
     }
     catch( SQLException se )  {
      System.out.println( "SQL Exception:" ) ;
    }