JDBC learning is easy to understand


foreword

The database is an independent program, and the Java application program is also an independent program, so how to realize the connection between the two? Here we use JDBC to achieve.


提示:以下是本篇文章正文内容,下面案例可供参考

2. How to realize the connection?

1. Realize the connection between JAVA and the database

1.1 First of all, we have to build a bridge (database connection Connection) between the database and the Java application, through which the communication and information interaction between the two can be realized.

1.2 So who will build the bridge? Then you have to find a construction team. In the computer, this construction team is a class (DriverManager). To complete the specific construction work, you must use the method of this class to create a database getConnenction(建立连接所需要资源)connection.

accomplish:Connection conn = DriverManager.getConnection(url: 建立连接所需要的资源);

1.3 Creating a connection (bridge) requires resources, so what resources are needed? The resource pointed to by the url is the resource we need. Here I will give an example: Suppose the MySQL user name is root, the password is 123456, and the database name is: student.

String username = "root";
String userpwd = "123456";
String dbname = "student";
String  url1="jdbc:mysql://localhost:3306/"+dbname;
String url2 ="?user="+username+"&password="+userpwd;
String url =url1+url2; 
Connection conn=DriverManager.getConnection(url); //创建连接对象(创建桥)            

The url corresponds to:

jdbc:mysql://hostname:mysql service port number/database name? parameter=value¶meter=value

jdbc:mysql are protocol and sub-protocol respectively.

1.4 So how to find the construction team (DriverManager)? We find the build team (DriverManager) through an agency that manages build teams. On a computer this governing body is called a driver java.sql.Driverand to get the build team is to load the driver

Class.forName(com.mysql.jdbc.Driver);

Among them, com.mysql.jdbc.Driver is the driver for MySQL connection.

In order to deal with the above problems, various database developers have specially developed a set of driver packages to realize the interaction between Java and databases for their own data systems. For example, for MySQL database, its driver package is: mysql-connector-java-5.1.47-bin. When we use it, we add this package to our project.

Through the previous steps, we have established the connection between the database and MySQL, code demonstration

建立数据库的连接的关键代码段如下://
   String driverName = "com.mysql.jdbc.Driver";         //驱动程序名
   String userName = "root";                            //数据库用户名
   String userPwd = "123456";                           //密码
   String dbName = "students";                           //数据库名
   String  url1="jdbc:mysql://localhost:3306/"+dbName;
   String url2 ="?user="+userName+"&password="+userPwd;
   String  url3="&useUnicode=true&characterEncoding=UTF-8";//访问数据库的汉字编码
   String url =url1+url2+url3;        //形成带数据库读写编码的数据库连接字
   Class.forName(driverName);                  //加载并注册驱动程序
   Connection conn=DriverManager.getConnection(url);  //创建连接对象

2. Realization of information exchange between the two after connection

2.1 Through the previous work, we already have the database connection object "bridge" conn, so how to realize information transmission at both ends of the bridge? That is, to transport materials back and forth. We can build a car to transport information back and forth on the bridge. On the computer, we call this car a database operation object.

Statement stmt = conn.createStatement(); //在桥conn上直接造一辆汽车

So what exactly does the car transport? Transport sql statements and execution results! There are usually two types of database operations, one is to query the database, and the other is to update the database (addition, deletion, modification).

1)查询数据库操作实现//
    String sql="selectname from stu";//查询SQL语句
    ResultSet rs=stmt.executeQuery(sql);  //执行查询SQL语句,并得到查询结果集合2)更新数据库(添加、删除、修改)操作实现//
    String sql2=“delete  stu”;         //删除记录SQL语句
    int n=stmt.executeUpdate(sql2);  //执行删除SQL语句,并返回实际删除记录的条数 

2.2 Note that I built the car first, and then moved the things to be transported to the car for transportation. So can we move the things to be transported to the car while building the car? sure!

1)查询数据库操作实现//
  String sql="select xh,name,cj from stu where cj>=60 and cj<=80" //查询SQL语句
  PreparedStatement pstmt= conn.prepareStatement(sql);
  RrsultSet rs=pstmt.executeQuery(); //执行,得到查询结果集合,注意,这里是空参数
2)更新数据库(添加、删除、修改)操作实现//
    String sql2=“delete stu where cj>=60 and cj<=80;         //删除记录SQL语句    
    PreparedStatement pstmt= conn.prepareStatement(sql2);
    int n=pstmt.executeUpdate();  //执行,返回实际删除记录的条数,注意,这里是空参数

Summarize

This is the general principle, the details of the specific method can be seen in this blog

JDBC study

Guess you like

Origin blog.csdn.net/inspireT/article/details/124406607