Something about the JDBC

JDBC concepts

  定义:JDBC(Java数据库连接,java数据库连接)
  是一种用于执行SQL语句的Java API.

JDBC defines the interface, implementation is realized by the major manufacturers of databases. JDBC is a specification standard java to access the database, specifically how to operate the database, you need a specific category, that is,Database-drivenEach vendor has its own database-driven, so we will only need to call some methods JDBC interface can be,
Here Insert Picture Description

How they implement JDBC:

Related Documents
Link: https: //pan.baidu.com/s/1IF4mXVjBrUO9cCboyBWCcA
extraction code: 0faw
copy the contents of this open Baidu network disk phone App, the operation more convenient oh
mysql jar package:
Link: https: //pan.baidu.com/s/1TOiJI8LAx_3AaYOwi21pJQ
extraction code: fejg

Quick links to master java and database

step:

    1. Import Driver jar package mysql-connector-java-5.1.37- bin.jar
      1. Copy mysql-connector-java-5.1.37- bin.jar libs directory to the next item
      2. Right -> Add As Library
    1. Registration drive
    1. Get a database connection object Connection
    1. Defined sql
    1. Get executed sql statement object Statement
    1. Execute sql, accept the return result
    1. Release resources

Started Reference Code:

package MT_Mike;
import java.sql.*;
public class JdbcTest {
	public static void main(String args[]) throws Exception {//要抛异常才能正常运行
		//1.导入jar包
		/*已经导入*/
		//2注册驱动
		Class.forName("com.mysql.jdbc.Driver");//固定mysql注册驱动格式
		//3.获取数据库连接对象
		Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "root");
		//第一个参数用于标识数据库的位置,第二个参数为数据库账号,第三个参数为数据库密码
		//4定义sql语句
		String st="update test set name='ssd' where id=1";//同数据库的一些语法一样
		//5.获取执行sql对象Statement
		Statement stmt=conn.createStatement();
		//6.执行sql
		int count=stmt.executeUpdate(st);//执行更新操作
		//7.执行
		System.out.println(count);
		//8释放资源
		stmt.close();//先释放statement
		conn.close();//后释放Connection
	}
}

Upgraded version of the print operation

package MT_Mike3;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

public class JdbcDemo3 {

	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		//1.注入驱动
		Class.forName("com.mysql.jdbc.Driver");
		//2.sql语句
		String sql="select * from test ";
		//3.建立connection 链接
		Connection con=(Connection) DriverManager.getConnection("jdbc:mysql:///db1", "root", "root");
		//4.执行sql对象的statement
		Statement st=(Statement) con.createStatement();
		//5.执行
		ResultSet result=st.executeQuery(sql);
		//6.打印
	//6.1
		while(result.next()) {
		int id=result.getInt(1);
		String name=result.getString(2);
		System.out.println(id+"==="+name);}
		//7.释放资源;
		result.close();
		st.close();
		con.close();
	}

}
Published 63 original articles · won praise 12 · views 4060

Guess you like

Origin blog.csdn.net/qq_45353823/article/details/103432230