One, ready to work
1. Open SQL Server service and support TCP/IP further confirmation tcpport
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmjk2ndi4mq==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
2. Import Sqljdbc4.jar package to project under Eclipse
Extract the downloaded Sqljdbc_4.0.2206.100_chs.exe to perform the decompression. Then select Sqljdbc_auth.dll for the appropriate version number under the. \microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\chs\auth path. Place it under the C:\Windows\System32.
To import a package to a project: Right-click your project to select Properties. Pop-up the following form, select Java Build Path
Import by some of the add actions on the right, select Add jars to place the Sqljdbc4.jar package below the project folder.
Second, connect the database (SQL Server)
String drivername = "Com.microsoft.sqlserver.jdbc.SQLServerDriver"; String Dburl = "JDBC:SQLSERVER://LOCALHOST:1433;DATABASENAME=LG"; Class.forName (drivername);//load this class//dbconn = Drivermanager.getconnection (Dburl);//windows identity authentication tring username = "sa"; String password = "xxxx";d bconn = drivermanager.getconnection (Dburl,username,password);
Third, the operation of the database
1, the ResultSet class to the database operation
Statement st = dbconn.createstatement (resultset.type_scroll_sensitive,//sensitive to data resultset.concur_updatable);// Updatable String sql = "SELECT * from student"; ResultSet result = st.executequery (SQL);//update 1th Line Name field Result.absolute (1); Result.updatestring ("Name", "LG"); Result.updaterow ();//Insert a record result.movetoinsertrow (); result.updatestring (1, "ln"); Result.updatestring (2, "male") ); Result.updateint (3,12); Result.updateint (4,80); Result.insertrow (); Result.movetoinsertrow ();//Delete a record Result.last (); Result.deleterow (); Result.absolute (0); St.close ();//Close Dialog dbconn.close ();//close connection again
2, Save the Connection database information to the Driver.properties file, and then use a GetProperty () method to obtain information
Driver.properties file:
Drivers=com.microsoft.sqlserver.jdbc.sqlserverdriverurl=jdbc:sqlserver://localhost:1433;databasename=lguser= Sapassword=xxxx
Note: The file must be placed below the root folder of the project, and there must be no extra spaces on either side of the
GetProperty () method:
public void GetProperty () {Properties prop = new Properties (), try {fileinputstream in = new FileInputStream ("Driver.proper Ties ");p rop.load (in);d rivername = Prop.getproperty (" drivers "); URL = prop.getproperty (" url "); userName = Prop.getproperty ("user");p Assword = Prop.getproperty ("PassWord");} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ()}}
3. Transaction processing
Set the database's own unsolicited commit to false. Run a SQL statement. In Con.commit (), it is also possible to pass Con.rollback () before returning to the state before the SQL statement is run.
<pre name= "code" class= "CPP" >con.setautocommit (false); st.executeupdate (SQL); Con.rollback (); Con.commit (); Con.setautocommit (TRUE);
4. Pre-query
String sql = "SELECT name, school number, major, birthplace from student where name =?"; PreparedStatement pre = con.preparestatement (String sql);p re.setstring (1, "Zhang Qiang"); ResultSet re = Pre.executequery ();
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Java Learning notes--JDBC and its applications