Use ODBC to connect MS SQL

xiaoxiao2021-03-06  102

import java.sql *;. public class Dao {public static void main (String [] args) {String url; String S_id; String S_name; String S_add; Connection con; Statement sql = null; ResultSet rs; try {Class.forName ("Sun.jdbc.odbc.jdbcodbcdriver"); // Load the JDBC-ODBC Bridge driver provided by Sun Company. The driver is responsible for registering DRIVERMANAGER} catch (Exception E1) {system.out.println (e1.tostring ()); system.out.println ("NO1");} try {url = "jdbc: odbc: student" Con = DriverManager.getConnection (URL); // Using the simplest connection method, no additional information is given outside of the data source name. // con = DriverManager.getConnection ("JDBC: ODBC: Student", "," "); // second method, you can give the username and password // DriverManager to process the load driver and create new The database connection provides hijacking; system.out.println ("Connection success"); DatabaseMetadata dbma = con.getMetadata (); // Get database basic information System.out.Println (dbma.geturl ()); system.out.println (dbma.getusername ()); system.out.println (dbma.isreadOnly ()); SQL = con.createstatement (); // statement is a container that performs declarations in a given connection; it contains two An important subtype: RS = SQL.ExecuteQuery ("SELECT S_ID, S_NAME, S_ADD from Student Where S_ID = 1"); while (rs.next ()) {

S_ID = rs.getstring ("s_id");

S_Name = rs.getstring ("s_name");

S_ADD = rs.getstring ("s_add");

System.out.println (S_ID);

System.out.println (S_Name);

System.out.println (S_ADD);

}

} catch (Exception E2) {system.out.println (e2.tostring ()); System.out.Println ("NO2");} try {sql.close ();} catch (sqlexception e) {system.out .println ("NO3");}}

}

转载请注明原文地址:https://www.9cbs.com/read-125961.html

New Post(0)