Use java to add, delete, modify, and query SQL server.
1 import java. SQL. connection; 2 import java. SQL. driverManager; 3 import java. SQL. preparedStatement; 4 import java. SQL. resultSet; 5 import java. SQL. SQLException; 6/** 7 * TestDemo. java 8 * use java to add, delete, modify, and query SQL server 9 * --------------------------- 10 * Database Name: test11 * Table Name: user12 * field: 13 * id: int (identity () 14 * userName: nvarchar15 * password: nvarchar16 * --------------------------- 17 * Some methods require try-cat Ch18 * throws20 */21 public class TestDemo {22/* 23 * for the sake of simplicity 19 * using the PreparedStatement object to operate the database 24 * has many advantages over the Statement 25 * security, performance... 26*27 * The PreparedStatement parameter index starts from 1. 28*29 * Some Common Methods 30 * ResultSet executeQuery (); // You can execute an SQL query and return the ResultSet object 31 * int executeUpdate (); // You can execute "add, delete, modify" and return 32 * boolean execute (); // You can execute any SQL statement. If the Result object is a Result object, true is returned. otherwise: false33 */34 private static PreparedStatement ps; 35 36 p Ublic static void main (String [] args) throws ClassNotFoundException, SQLException {37 // url38 String url for accessing the database = "jdbc: sqlserver: // localhost: 1433; databasename = test "; 39 // load the driver 40 Class. forName ("com. microsoft. sqlserver. jdbc. SQLServerDriver "); 41 // establish a Connection 42 Connection conn = DriverManager. getConnection (url, "username", "password"); 43 44/* insert */45 ps = conn. prepareStatement ("insert into [user] values (?,?) "); 46 ps. setString (1, "Xiaohong"); // set userName to: Xiaohong 47 ps. setString (2, "Xiaohong password"); // set password to: Xiaohong password 48 if(ps.exe cuteUpdate ()> 0) {// if the number of affected rows returned is greater than 0, 49 System. out. println ("added a record"); 50} 51 52/* update */53 ps = conn. prepareStatement ("update [user] set password =? Where userName =? "); 54 ps. setString (1, "pwd"); // set the password to pwd55 ps. setString (2, ""); // set the update condition, userName = 56 56 System. out. println ("Updated:" paips.exe cuteUpdate () + "records"); 57 58/* query */59 ps = conn. prepareStatement ("select * from [user]"); // query all user60 ResultSet rs = ps.exe cuteQuery (); 61 while (rs. next () {// if there is no next record, false is returned, and the loop jumps out 62 // System. out. println ("name:" + rs. getString ("username") + ", password:" + rs. getString ("password"); 63 String username = rs. getString ("username"); 64 String pwd = rs. getString ("password"); 65 System. out. println ("name:" + username + ", password:" + pwd); 66} 67 68/* delete */69 ps = conn. prepareStatement ("delete [user]"); // This will delete all records in the user 70 System. out. println ("deleted:" cmdps.exe cuteUpdate () + "records"); 71 72/* close to release resources */73 rs. close (); 74 ps. close (); 75 conn. close (); 76} 77}