sqlserver java database connection

Download jdbc:

https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=11774

 

Database Connectivity core code:

package test;
 

the java.sql.Connection Import; 
Import the java.sql.DriverManager; 
Import the java.sql.ResultSet; 
Import java.sql.SQLException; 
Import the java.sql.Statement; 
public class DbUtils { 
    public static void main (String [] args) { 
        {the try 
            the Class.forName ( "com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
        } the catch (a ClassNotFoundException E) { 
            System.err.println ( "drive not found"); 
        } 
        System.out.println ( "database-driven success "); 
        the try { 
            String connectDB =" jdbc: sqlserver: // localhost \\ SQLEXPRESS: 1434; DatabaseName = MXJ "; 
            String the user =" SA "; 
            // username and password here as long as the attention can not be wrong
            Password = String "MXJmxj514"; 
            Connection CON = DriverManager.getConnection (connectDB, the User, password); 
            // connect to the database objects 
            System.out.println ( "Database connection successful"); 
            of Statement stmt = con.createStatement (); 
            // create a SQL command object // create a table 
            System.out.println ( "start creating the table"); 
// String query1 = "TABLE1 drop the table"; 
            // create table SQL statement 
            String query = "create table TABLE1 ( ID NCHAR (4 ) a PRIMARY KEY the NOT NULL, the NCHAR NAME (10), T "+" the NCHAR the EL (. 11)) "; 
        // stmt.executeUpdate (Query1); 
            // execute SQL command object 
            stmt.executeUpdate (query);
            // execute SQL command objects
            System.out.println ( "Table successfully created");  
            // input data 
            System.out.println (" start insert data ");
            String a1 = "INSERT INTO TABLE1 VALUES ( '0001', 'Li Hua', '13933209898')"; 
            // insert the data SQL statements 
            String a2 = "INSERT INTO TABLE1 VALUES ( '0002', 'WANG', '13,698,760,987') "; 
            String A3 =" the INSERT the INTO TABLE1 the VALUES ( '0003', 'Zhang brother', '1786308096') "; 
            stmt.executeUpdate (A1); 
            // execute SQL command object 
            stmt.executeUpdate (a2); 
            stmt.executeUpdate (a3); 
            System.out.println ( "Inserting data success"); 
            // read data 
            System.out.println ( "start reading"); 
            ResultSet rs = stmt.executeQuery ( "the SELECT * the FROM TABLE1"); 
            // Returns the SQL statement query result set (the set) 
            // cycle of the output of each record 
            while (rs.next ()) {
                The output of each field // 
                System.out.println (rs.getString ( "ID") + "\ T" + rs.getString ( "NAME")); 
            } 
            System.out.println ( "read complete"); 
            stmt.executeUpdate ( "update dbo.TABLE1 set NAME = ' LIU' where ID = '0002'"); 
            // if not followed by the conditions where, the value field is updated all columns 
            System.out.println ( "modify data complete "); 
            RS = stmt.executeQuery (" the FROM TABLE1 the SELECT * "); 
            // returns the query result set of SQL statements (set) 
            // cycle of the output of each record 
            the while (rs.next ()) { 
                // for each output field 
                System.out.println (rs.getString ( "ID") + "\ t" + rs.getString ( "NAME")); + "\ t" + rs.getString("NAME"));
            }
            String sql = "delete from TABLE1 where id='0001'"; 
            stmt.executeUpdate (SQL); 
            System.out.println ( "complete delete data"); 
            RS = stmt.executeQuery ( "the FROM TABLE1 the SELECT *"); 
            / / SQL statements that return query result set (set) 
            // cycle of the output of each record 
            the while (rs.next ()) { 
                // for each output field 
                System.out.println (rs.getString ( "ID") + "\ t "+ rs.getString (" NAME ")); 
            } 
            // close the connection 
            stmt.close (); 
            // close connection command object 
            con.close (); 
            // close the connection 
        } the catch (SQLException E) { 
            E.printStackTrace (); 
            of System.out.print (e.getErrorCode ()); 
            System.out.println ( "database connection error");
            System.exit(0);
        }
    }
}
​

Guess you like

Origin blog.csdn.net/weixin_41808843/article/details/88979715