当前位置:网站首页>JDBC introduction

JDBC introduction

2022-07-06 15:11:00 Hand pluckable Xinchen

1,JDBC Concept

Java Database Connectivity(JDBC):Java Access to the database solution .

JDBC Defines a set of interfaces , That is to access the general database API, Different database manufacturers implement these interfaces according to the characteristics of their respective databases .

JDBC You want to access different databases in the same way , Let the specific database operation have nothing to do with the implementation of the database manufacturer , Thus in

Easily switch between different databases .

2,JDBC Related class interface

        DriverManager: Driver management
        Connection: Connection interface
        Statement: Statement object interface
        ResultSet: Result set interface
3,JDBC working principle
        (1)Class.forName()                  The load driver
Class.forName("com.mysql.cj.jdbc.Driver");
        (2)DriverManager()                  obtain Connection Connect
String url = "jdbc:mysql://localhost:3306/dayone?serverTimezone=GMT";
String user = "root";
String pwd = "123456";
Connection conn = DriverManager.getConnection(url, user, pwd);
        (3) establish Statement                    perform SQL sentence
String sql = "insert into student(sname,birthday,ssex,classid) values(' Zhang San ','2022-1-1',' male ','100')";
Statement statm = conn.createStatement();
        (4) return ResultSet                      Query results   
int ret = statm.executeUpdate(sql);       // perform DML sentence ( add to , Delete , modify ), Return value int
//ResultSet rs = stmt.executeQuery(sql);    // perform DQL sentence ( Inquire about ), Return value ResultSet
if(ret > 0) {
	System.out.println(" Execution succeeded ");
}else {
	System.out.println(" Execution failed ");
}

        (5) Release resources

statm.close();
conn.close();

原网站

版权声明
本文为[Hand pluckable Xinchen]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060919160049.html