JDBC learning (I)

JDBC learning (I)

concept

The so-called English abbreviation means: Java database connectivity, that is, the connection of Java database, which uses Java language to operate the database

essence

In short, it is a set of rules for operating relational databases defined by the JDBC company, so it can be seen that it is like an interface, and then manufacturers such as MySQL or Oracle provide "implementation classes" - drive jar packages, so that Java can be used to operate databases

java. SQL: all interfaces and classes related to JDBC accessing database javax SQL: database expansion package, which provides additional functions of database. For example, the connection pool database driver is provided by major database manufacturers and needs to be downloaded additionally. It is a class implemented for the JDBC interface

step

1. Import the driver jar package (create a new directory in the project, copy and paste the jar package into this directory)

2. Register the driver with Java (Note: the version that has been widely used since JDBC 3. You can use it directly without registering the driver. The sentence class. Forname can be omitted. It is recommended to write this statement)

MysqL例: Class.forName("com.MysqL.jdbc.Driver");
Oracle例:Class.forName("oracle.jdbc.driver.OracleDriver");

3. Get the connection object of the database

例: Connection con =Drivermanger.getConnection(url:"jdbc:MysqL://locallhost:3306/数据库名",user"",password:"")
	***Drivermanger是驱动管理对象***
	功能:1.注册驱动     Class.forName("com.MysqL.jdbc.Driver");
		(其实是根据deregisterDriver这个注册驱动的方法来实现的;且从MysqL5之后的驱动jar包j会自动进行注册  驱动)
	    2.获取数据库连接
	      .参数:url  user  password
		  .MysqL语法:jdbc:MysqL://ip地址(域名):端口号/数据库名称
		
	***Connection是数据库连接对象***
		1.获取执行sql的对象
		  .Statement createStatement()
		  .PreparedStaement preparStatement(String sql)
		2.管理事务 
		  .开启事务:setAutoCommit(boolean autoCommit): 调用该方法设置参数为false,即开启事务
		  .提交事务:commit()
		  .回滚事务:rollback()

4. Define SQL statements

例: String sql =" /*写上你要做的sql语句*/";

5. Get the object that executes the SQL statement, that is, statement

例: Statement stmt = con.createStatement();
	Statement是执行sql的对象

6. Execute SQL and accept the returned results

例: int count = stmt.executeUpdate(sql);
  /*执行DML语句(常用)、DDL语句*/
    boolean execute(String sql):可以执行任意的sql (不常用)
    ResultSet excuteQuery(String sql):执行DQL语句;返回结果集对象

7. Treatment results

例: System.out.println(count);//受影响的行数,然后根据受影响的行数来判断DML语句是否执行成功

8. Release resources

例: stmt.close();
	con.close();

9. Resultset and preparstatement objects

ResultSet是结果集对象,即封装查询结果的
	**boolean next()方法**:游标向下移动一行;返回值为boolean
	**getXxx(参数)方法**:获取数据;Xxx代表数据类型;比如:getInt()、getString()、getDouble()
						通过传参可以进行获取相应的数据;比如:int中的数据就是列的编号,String中的数据就是列的名称
	正确的步骤应该是:1.游标向下移动一行
				   2,判断是否有数据(利用循环简化判断)
				   3.获取数据
PreparStatement是执行sql的对象,功能比父类Statement更为强大

Example code

The following is the code in the rookie tutorial!

Add, delete, modify and query

About Preparedstatement

Benefits of preparedsatement

To use Preparedstatement:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>