1 PackageAA;2 ImportJava.sql.*;3 Public classGold {4 Public Static voidMain (string[] args) {5String jdriver= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";//SQL Database Engine6String connectdb= "JDBC:SQLSERVER://127.0.0.1:1433;DATABASENAME=JXGL";//Data Source7 8 Try9 {TenClass.forName (Jdriver);//loads the database engine and returns the class for the given string name One}Catch(classnotfoundexception e) A { - //e.printstacktrace (); -System.out.println ("Failed to load Database engine"); theSystem.exit (0); - } -SYSTEM.OUT.PRINTLN ("Database-driven Success"); - + Try - { +String user= "SA"; AString password= "123456"; atConnection con=drivermanager.getconnection (Connectdb,user,password);//Connecting database Objects -SYSTEM.OUT.PRINTLN ("Connection database succeeded"); -Statement stmt=con.createstatement ();//To create a SQL command object - - //Create a table -System.out.println ("Start creating table"); inString query= "CREATE Table TABLE1 (ID NCHAR (2), NAME NCHAR (10))";//To create a table SQL statement -Stmt.executeupdate (query);//Execute SQL Command object toSYSTEM.OUT.PRINTLN ("Table creation succeeded"); + - //input Data theSystem.out.println ("Start inserting data"); *String a1= "INSERT into TABLE1 VALUES (' 1 ', ' Asahi Brothers ')";//Insert Data SQL statement $String a2= "INSERT into TABLE1 VALUES (' 2 ', ' viagra ')";Panax NotoginsengString a3= "INSERT into TABLE1 VALUES (' 3 ', ' brother Zhang ')"; -Stmt.executeupdate (A1);//Execute SQL Command object the Stmt.executeupdate (A2); + Stmt.executeupdate (A3); ASystem.out.println ("Insert data Success"); the + //reading Data -System.out.println ("Start reading data"); $ResultSet rs=stmt.executequery ("SELECT * from TABLE1");//return SQL statement query result set (collection) $ //loop out each record - while(Rs.next ()) - { the //Output each field -System.out.println (rs.getstring ("ID") + "\ T" +rs.getstring ("NAME")));Wuyi } theSYSTEM.OUT.PRINTLN ("Read Complete"); - Wu //Close Connection -Stmt.close ();//Close Command object connections AboutCon.close ();//To close a database connection $ } - Catch(SQLException e) - { - e.printstacktrace (); A //System.out.println ("Database connection Error"); +System.exit (0); the } - } $}
Eclipse-sql Server 2008 Connection (RPM)