jdbc测试

增删改:

package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.PreparedStatement;
public class test {
	//jdbc:microsoft.sqlsever://localhost:1433:DatabassName=test
	private static final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=test";
	private static final String user = "sa";
	private static final String password = "123";
	private static Statement stmt;
	private static PreparedStatement pstmt;
	@SuppressWarnings("null")
	public static void up(){
		// TODO Auto-generated method stub
		Connection connection =  null;
		PreparedStatement pstmt = null;
		try {
		//a、导入驱动
		//Class.forName("com.mysql.cj.jdbc.Driver");//加载具体驱动(mysql)
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		//b、建立连接
		 connection = DriverManager.getConnection(url, user, password);

/*Statement
		//		c、执行sql语句
		Statement stmt = connection1.createStatement() ;
//增
//		String sql = "insert into test values(1,'dsd',44)";
//删
//		String sql =" delete FROM test where num= 1";
//改
		String sql =" update test set num=3 where name='dsd'";
		int count = stmt.executeUpdate(sql);	  */
		
		
		
		//prepaStatement
		//String sql="insert into test values(3,'ad',42)";//普通
		String sql="insert into test values(?,?,?)";//预编码
		pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, 4);//学号
		pstmt.setString(2, "zzs");//姓名
		pstmt.setInt(3, 36);//年龄
		int count = pstmt.executeUpdate();
		if(count > 0) {
			System.out.println("操作完成");
		}
		}catch(ClassNotFoundException e) {
			e.printStackTrace();
		}catch(SQLException e) { 
			e.printStackTrace();
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
			if(stmt!= null)stmt.close();	
			if(connection!= null)connection.close();
			}catch(SQLException e){
				e.printStackTrace();
			}
		}
		
	}


public static void main(String[] args) {
	up();
}

}

 查询:

 1 package jdbc;
 2 
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5 import java.sql.PreparedStatement;
 6 import java.sql.ResultSet;
 7 import java.sql.SQLException;
 8 import java.sql.Statement;
 9 
10 public class rquest {
11 
12         // TODO Auto-generated method stub
13         private static final String url = "jdbc:sqlserver://localhost:1433;DatabaseName=test";
14         private static final String user = "sa";
15         private static final String password = "123";
16         private static Statement stmt;
17         private static PreparedStatement pstmt;
18         public static void  up(){
19             // TODO Auto-generated method stub
20             Connection connection =  null;
21             ResultSet rs = null;
22             PreparedStatement pstmt= null;
23             try {
24             //a、导入驱动
25             //Class.forName("com.mysql.cj.jdbc.Driver");//加载具体驱动(mysql)
26                 Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
27             //b、建立连接
28             connection = DriverManager.getConnection(url, user, password);
29 String sql;
30             //            z执行sql语句
31 //模糊查询A,B才用    stmt = connection1.createStatement() ;
32 //            String sql ="select * from test";
33 //模糊查询A 根据相关信息查询,比如所含字母
34 //            String sql = "select *from test where name like '%s%'";
35 //模糊查询B
36             /*String Name = "s" ;
37             String sql ="select * from test where name like '%"+Name+"%'";*/    
38             //String sql = "select num,name,age from test";
39 //            rs = stmt.executeQuery(sql);        
40 //            模糊查询c    ,prepareStatemnt
41 
42             sql ="select * from test where name like ?";        
43             pstmt = connection.prepareStatement(sql);
44             pstmt.setString(1, "%z%");
45             rs = pstmt.executeQuery();
46             while(rs.next()) {
47                 int num = rs.getInt("num");
48                 String name = rs.getString("name");
49                 int age = rs.getInt("age");
50                 System.out.println("num---"+num+"//name---"+name+"//age----"+age);
51             }
52             }catch(SQLException e) {
53                 e.printStackTrace();
54             }catch(ClassNotFoundException e) {
55                 e.printStackTrace();
56             }catch(Exception e) {
57                 e.printStackTrace();
58             }finally {
59                 try {
60                 if(rs!=null)rs.close();
61                 if(stmt!= null)stmt.close();    
62                 if(connection!= null)connection.close();
63                 }catch(SQLException e){
64                     e.printStackTrace();
65                 }
66             }
67             
68         }
69     
70 
71 public static void main(String[] args) {
72     up();
73     
74 }
75 }
查询
原文地址:https://www.cnblogs.com/mi-9/p/12734870.html