To summarize, connecting to a SQL Server database requires the following steps:
1. Import Drive Jar Package: Sqljdbc.jar
2. Load and register the driver
3. Setting the connection path
4. Load and register the drive
5. Connect to the database
6. Operational database
7. Close the connection
The code is as follows:
* Connect Database *******************
1 Packagezj6_test;2 ImportJava.sql.*;3 Public classZj6_3 {4 /**5 * Use statement interface to implement additions and deletions to the database.6 */7 Private Static FinalString driver=8"Com.microsoft.sqlserver.jdbc.SQLServerDriver";//loading and registering drivers9 Private Static FinalString url=Ten"Jdbc:sqlserver://localhost:1433;databasename=bank";//Setting the connection path One Public Static voidMain (string[] args) { AConnection con=NULL;//establishing a connection pool -Statement Sta=NULL; - Try { theClass.forName (driver);//load and register the driver -Con=drivermanager.getconnection (URL, "sa", "sa");//connecting to a database - //adding data to the Database bank table -Sta=con.createstatement ();//gets the object that the reference to the statement interface points to by using the Createstatement () method +Sta.execute ("INSERT into account values" (' Caocao ', ' 420106198205188777 ', ' 2011-07-07 ')); -Sta.close ();//Close the Ststement object + // //modify the data in table account_id to 7 A //String account_name= "Cao Zhi"; at //String code= "420683199203212111"; - //String open_time= "2011-07-10"; - //sta=con.createstatement (); - //String updatesql= "Update account set Account_name= '" +account_name+ "', code= '" +code+ "', Open_time= '" +open_tim E+ "' Where account_id=" +7; - // - //Sta.execute (updatesql); in //sta.close (); - // //Delete account_id=7 records from the account table to //sta=con.createstatement (); + //String delsql= "Delete from account where account_id=" +7; - //sta.executeupdate (delsql); the //sta.close (); * //Con.close (); $}Catch(Exception e) {Panax Notoginseng e.printstacktrace (); - } the } +}
Operation Result:
Of course, in practical applications, in order to embody the characteristics of Java encapsulation, often the repeated use of the method encapsulated in a class, each time directly to use it.
Here is a wrapper class:
1 Packagezj6_test;2 ImportJava.sql.*;3 Public classDBManager {4 /**5 * Create specialized custom classes to make connections, close objects, and close connections6 */7 Private Static FinalString driver=8"Com.microsoft.sqlserver.jdbc.SQLServerDriver";9 Private Static FinalString url=Ten"Jdbc:sqlserver://localhost:1433;databasename=bank"; One Private Static FinalString user= "SA"; A Private Static FinalString pwd= "SA"; - Private StaticConnection con=NULL;//To establish a connection pool object - //establish a connection to the database the Public StaticConnection Getcon () { - Try { - Class.forName (driver); -con=drivermanager.getconnection (url,user,pwd); +}Catch(Exception e) { - + e.printstacktrace (); A } at returncon; - } - //Close Connection - Public Static voidClosecon (Connection con) { - Try { - if(con!=NULL){ in con.close (); - } to}Catch(SQLException e) { + e.printstacktrace (); - } the } * //Close ResultSet $ Public Static voidCloseresultset (ResultSet rst) {Panax Notoginseng Try { - if(rst!=NULL){ the rst.close (); +rst=NULL; A } the}Catch(Exception e) { + e.printstacktrace (); - } $ } $ //Close Statement - Public Static voidclosestatement (PreparedStatement pst) { - Try { the if(pst!=NULL){ - pst.close ();Wuyipst=NULL; the } -}Catch(Exception e) { Wu e.printstacktrace (); - } About } $}
Limited capacity, some local local claims are not professional, but also hope to criticize!
Explain how to connect SQL Server database, and use statement interface to implement additions and deletions to the database.