JDBC的DQL操作

 1 import java.sql.Connection;
 2 import java.sql.DriverManager;
 3 import java.sql.ResultSet;
 4 import java.sql.SQLException;
 5 import java.sql.Statement;
 6 
 7 import org.junit.jupiter.api.Test;
 8 
 9 public class DML
10 {
11     Connection conn =null;
12     Statement st = null;
13 @Test
14 void selectTest()
15 {
16     //sql语句
17         String sql="SELECT * FROM product";
18         ResultSet rs= null;
19         try {
20             //获取连接对象
21             conn =DriverManager.getConnection("jdbc:mysql:///test", "root", "123456");
22             //获取语句对象
23             st =conn.createStatement();
24             //执行sql语句
25             rs = st.executeQuery(sql);
26             while(rs.next())
27             {
28             System.out.println(rs.getString("productName"));
29             }
30             } catch (Exception e) {
31             // TODO Auto-generated catch block
32             e.printStackTrace();
33         }
34         //释放资源
35             try {
36                 if(rs!=null)
37                 rs.close();
38                 if(null!=st)
39                 st.close();
40                 if(conn != null)
41                 conn.close();
42 
43             } catch (Exception e) {
44                 // TODO Auto-generated catch block
45                 e.printStackTrace();
46             }
47 }
demo_DQL


 

猜你喜欢

转载自www.cnblogs.com/Sly-Mi/p/9362614.html
DQL