步骤:
一、导驱动包包mysql-connector-java-8.0.11.jar
二、定义四大参数driverClassName、url、user、password
三、加载驱动类 Class.forName(driverClassName);
四、得到connection
Connection connection = DriverManager.getConnection(url, user, password);
五、得到Statement
Statement statement = connection.createStatement();
六、通过Statement执行executeUpdate方法进行增、删、改操作,执行executeQuery方法进行查询
七、executeQuery查询返回结果集resultSet进行解析取值等
八、倒关
package test2;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
public class TestJDBC {
@Test
public void test() throws ClassNotFoundException, SQLException{
//四大参数
String driverClassName="com.mysql.cj.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/mydb3?useSSL=false&serverTimezone=GMT-8";
String user="root";
String password="123";
//加载驱动类
Class.forName(driverClassName);
//得到connection
Connection connection = DriverManager.getConnection(url, user, password);
//连接成功,执行增、删、改操作
//得到Statement
Statement statement = connection.createStatement();
//通过statement执行executeUpdate方法
String sql="INSERT INTO test VALUES (58,5.5,6,'ZHANS','DEFSSA','SADFDSAFSDAFSDA',"
+ "'DSAFDSAFDSAFDSAF','2018-6-25','14:00:00','2018-6-25 14:00:00',55)";
sql="UPDATE test SET d='bbs',b=9.99 WHERE tid=58";
sql="DELETE FROM test WHERE tid=55";
int sum=statement.executeUpdate(sql);
System.out.println(sum);
statement.close();
connection.close();
}
@Test
public void testQurity() throws ClassNotFoundException, SQLException{
//四大参数
String driverClassName="com.mysql.cj.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/exam?useSSL=false&serverTimezone=GMT-8";
String user="root";
String password="123";
//加载驱动类
Class.forName(driverClassName);
//得到connection
Connection con = DriverManager.getConnection(url, user, password);
//得到statement
Statement sta = con.createStatement();
//执行executeQuery方法查询得到resultSet结果集
String sql="SELECT * FROM emp";
ResultSet rs=sta.executeQuery(sql);
//解析resultSet,有一个行光标的,next,移动行光标,没有行了返回false,rs.getXxx()取值
while(rs.next()){
int no=rs.getInt(1);
String name=rs.getString("ename");
String job= rs.getString("job");
System.out.println(no+", "+name+", "+job);
}
//用完了要关闭的
rs.close();
sta.close();
con.close();
}
}
以上编写不规范,规范化编程如下
package test3;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
public class TestJDBC {
@Test
public void test() {
// 四大参数
String driverClassName = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/mydb3?useSSL=false&serverTimezone=GMT-8";
String user = "root";
String password = "123";
Connection connection = null;
Statement statement = null;
try {
// 加载驱动类
Class.forName(driverClassName);
// 得到connection
connection = DriverManager.getConnection(url, user, password);
// 连接成功,执行增、删、改操作
// 得到Statement
statement = connection.createStatement();
// 通过statement执行executeUpdate方法
String sql = "INSERT INTO test VALUES (58,5.5,6,'ZHANS','DEFSSA','SADFDSAFSDAFSDA',"
+ "'DSAFDSAFDSAFDSAF','2018-6-25','14:00:00','2018-6-25 14:00:00',55)";
sql = "UPDATE test SET d='bbs',b=9.99 WHERE tid=58";
sql = "DELETE FROM test WHERE tid=55";
int sum = statement.executeUpdate(sql);
System.out.println(sum);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
@Test
public void testQurity() {
// 四大参数
String driverClassName = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/exam?useSSL=false&serverTimezone=GMT-8";
String user = "root";
String password = "123";
Connection con = null;
Statement sta = null;
ResultSet rs = null;
try {
// 加载驱动类
Class.forName(driverClassName);
// 得到connection
con = DriverManager.getConnection(url, user, password);
// 得到statement
sta = con.createStatement();
// 执行executeQuery方法查询得到resultSet结果集
String sql = "SELECT * FROM emp";
rs = sta.executeQuery(sql);
// 解析resultSet,有一个行光标的,next,移动行光标,没有行了返回false,rs.getXxx()取值
while (rs.next()) {
int no = rs.getInt(1);
String name = rs.getString("ename");
String job = rs.getString("job");
System.out.println(no + ", " + name + ", " + job);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 用完了要关闭的
try {
if (rs != null)
rs.close();
if (sta != null)
sta.close();
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}