Differences

This shows you the differences between two versions of the page.

Link to this comparison view

javasqlserver [2014/10/26 01:52] (current)
Line 1: Line 1:
 +==== JDBC to connect to SQL server ====
 +<code lang="​java">​
 +import java.sql.*;
 +
 +public class ConnectURL {
 +
 + public static void main(String[] args) {
 +
 + // Create a variable for the connection string.
 + String connectionUrl = "​jdbc:​sqlserver://​SC068568\\SQLEXPRESS;"​ +  
 + "​user=SA;"​ + 
 + "​password=oracle";​
 +
 + System.out.println("​connectionUrl : "​+connectionUrl);​
 + // Declare the JDBC objects.
 + Connection con = null;
 + Statement stmt = null;
 + ResultSet rs = null;
 +
 + try {
 + // Establish the connection.
 + Class.forName("​com.microsoft.sqlserver.jdbc.SQLServerDriver"​);​
 + con = DriverManager.getConnection(connectionUrl);​
 +
 + // Create and execute an SQL statement that returns some data.
 + String SQL = "​SELECT * FROM FDCA.dbo.Asset";​
 + stmt = con.createStatement();​
 + rs = stmt.executeQuery(SQL);​
 + ResultSetMetaData rsmd = rs.getMetaData();​
 + for (int i=1; i<​=rsmd.getColumnCount();​ i++) {
 + System.out.print(rsmd.getColumnName(i)+","​);​
 + }
 + System.out.println();​
 + // System.out.println(rs.getRow());​
 +
 + // Iterate through the data in the result set and display it.
 + while (rs.next()) {
 + System.out.println(rs.getRow() + " : " +rs.getString(1)+"​ "​+rs.getString(2));​
 + }
 +      }
 +
 +      // Handle any errors that may have occurred.
 +      catch (Exception e) {
 +         ​e.printStackTrace();​
 +      }
 +      finally {
 +         if (rs != null) try { rs.close(); } catch(Exception e) {}
 +         if (stmt != null) try { stmt.close();​ } catch(Exception e) {}
 +         if (con != null) try { con.close();​ } catch(Exception e) {}
 +      }
 +   }
 +}
 +</​code>​
 +----
 +  * [[javainfo|Back to Java]]
  
javasqlserver.txt ยท Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0