Connect the SQL Server database in Java (example)

xiaoxiao2021-03-06  37

1: Install the JDBC driver of SQL Server, configure classpath / * * @ (#) jdbcbean.java * JDBC And ResultSermetata * Copyright 2004 CFZQ, Inc. All Rights Reserved. * @Author fan * @version 1.0, 21/10 / 04 * @since java.sql. *; Public class jdbcbean {// string sdbdriver = "sun.jdbc.odbc.jdbcodbcdriver"; / * ODBC connection * / string sdbdriver = "com.microsoft.jdbc . SQLSERVER.SQLSERVERDRIVER "; / * Drive string * / // String sconnstr =" jdbc: odbc: dbtest "; string sconnstr =" JDBC: Microsoft: SQLServer: // localhost: 1433; databasename = dbtest "; / * connection Database string * / connection conn = null; resultset = null; public jdbcbean () {try {class.forname (sdbdriver); / * Registration driver * /} catch (Exception E) {

E.PrintStackTrace (); / * Capture exception * /}} public resultSet ExecuteQuery (String SQL) / * Establish a connection to get the result set * / {= null; try {conn = drivermanager.getConnection (Sconnstr, "SA", "201"); statement stmt = conn.createstatement (); rs = stmt.executeQuery (SQL);} catch (sqlexception ex) {system.err.println ("AQ.ExecuteQuery:" ex.getMessage ()); Return Rs;} public static void main (string [] args) {try {jdbcbean jdbc1 = new jdbcbean (); resultset = jdbc1.executeQuery; while (rs.next ()) { System.out.print (rs.getstring); system.out.print (rs.getstring ("book_name")); system.out.print (rs.getstring ("book_author"); system. Out.print (rs.getstring ("book_press"); system.out.print (rs.getfloat); system.out.println (rs.getstring ("book_date");} system.out .println ("######################################################### #### "); ResultSetMetadata RSMD1 = rs.getMetata (); int ccount1 = rsmd1.getColumnCount (); system.out.println (ccou NT1); for (int i = 1; i <= ccount1; i ) {string cl1 = rsmd1.getColumnlabel (i); system.out.println (CL1);} for (int i = 1; i <= ccount1; i ) {string SN1 = rsmd1.getschemaname (i); system.out.println (SN1 ": i " sn the end ... ");} for (int i = 1; i <= ccount1; i ) {String TN1 = rsmd1.gettablename (I); System.out.Println (TN1 ": i " TN the end ... ");} for (int i = 1; i <= ccount1; i ) {string CTN1 = rsmd1.getColumnTypename (i); system.out.println (CTN1 ":"

i "CTN The end ...");}}}} catch (exception _ex) {}}} This example corresponds to the database DBTEST, Table T_book, field book_id => int, book_name, book_author, book_press => varchar, book_price => float, book_date => datetime

Types of

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

New Post(0)