Java is much easier to connect to a database than other languages (such as C, C ++, etc.), so how do you connect?

1. Import JAR package: HERE I have 3 versions can be linked by clicking:Pan.baidu.com/s/1BRZiXIHP…2. Create a new folder and put the JAR package in it, as shown in the picture below:3. Next, place the mouse pointer over the name of your project, right-click –> Properties, and click Add ars…4. Select the jar package you just imported, click OK, and then click Apply and Close.5. The connection code between Java and SQLserver is as follows:

import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class DBManager { private Connection con; private Statement sta; private ResultSet rs; / * * * * * * * * * * * * * * * * * * * * static block can improve the efficiency of * * * * * * * * * * * / static {try {class.forname (" com. Microsoft. Essentially. JDBC. SQLServerDriver ");  } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }} /*** load driver */ public Connection getConnection(){**/****1433 is your own SQLserver port number (default is 1433)*********/ * * / * * * * * * * * * * * * * * DatabaseName is what you want to connect the database name * * * * * * * * * / url String = "JDBC: essentially: / / localhost: 1433; DatabaseName=yun4jbookSY1"; Try {/ * * the first sa is essentially a user name, you the second one is the user name of the password. * * * / con = DriverManager getConnection (url, "sa", "sa"); sta = con.createStatement(); System.out.println(" link succeeded "); } catch (SQLException e) {system.out.println (" connection failed "); e.printStackTrace(); } return con; } /*public DBManager() { List<Connection> list = new ArrayList<Connection>(); for (int i = 0; i < 5; i++) { list.add(this.getConnection()); } this.con = list.get(0); }*/ public int update(String sql){ int row = -1; con = getConnection(); try { row = sta.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ this.close(); } return row; } public ResultSet query(String sql){ con = getConnection(); try { rs = sta.executeQuery(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return rs; } public void close(){ try { if (rs ! = null) { rs.close(); rs = null; } if (sta ! = null) { sta.close(); sta = null; } if (con ! = null) { con.close(); con = null; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }}}Copy the code

Note: make sure your port number and database name, username and password are the same as those for your SQLserver!

I just started to operate the public account, I am currently working in a bank, interested in my new public account, welcome to exchange!