/*
*
*此博客可以让刚开始入门新手们更快的掌握jdbc的操作
*
*/
开发工具:idea
数据库:mysql
jar包:junit-4.10
mysql-connector-java-8.0.18
junit-4.10下载
mysql-connector-java-8.0.18下载
注意1:jdbc的驱动因为使用的是mysql-connector-java-8.0.18,所以为(“com.mysql.cj.jdbc.Driver”),而不是(“com.mysql.jdbc.Driver”);
注意2:数据库连接的url是"jdbc:mysql://localhost:3306/jdbctest? serverTimezone=UTC",建议加上? serverTimezone=UTC,避免出现时间差的问题
数据库结构
这里我们新建一个Java项目:JDBCDemo-csdn
接着创建一个包
com.jdbc.java.demo;
创建lib文件夹,并导入需要的jar包
包里面创建类JDBCDemo1
package com.jdbc.java.demo;
import org.junit.Test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/*
*
* 查询数据表
*
*
* */
public class JDBCDemo1 {
@Test
public void demo1(){
try
{
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//创建连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest? serverTimezone=UTC", "root", "010126");
String sql = "select * from user";
Statement stmt = conn.createStatement();
ResultSet resultSet = stmt.executeQuery("select*from user");
//遍历,打印输出
while (resultSet.next()) {
int uid = resultSet.getInt("uid");
String username = resultSet.getString("username");
String password = resultSet.getString("password");
String name = resultSet.getString("name");
System.out.println("success");
System.out.println(uid + " " + username + " " + password + " " + name);
}
resultSet.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
添加操作
@Test
/*
* 保存操作
*
* */
public void demoInsert(){
Connection con=null;
Statement stmt=null;
try {
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获得连接
con= DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest? serverTimezone=UTC", "root", "010126");
//获得sql语句的对象
stmt=con.createStatement();
int i=stmt.executeUpdate("insert user values(null,'eee','123','网六')");
//修改语句大于一,说明成功
if(i>0){
System.out.println("success");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt=null;
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con=null;
}
}
}
删除操作
@Test
/*删除操作
*
*
* */
public void demoDelete(){
Connection con=null;
Statement stmt=null;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
con= DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest? serverTimezone=UTC", "root", "010126");
stmt=con.createStatement();
String sql="delete from user where uId>=4";
int i=stmt.executeUpdate(sql);
if(i>0){
System.out.println("success");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt=null;
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con=null;
}
}
}
修改操作
@Test
/*
* 修改操作
*
* */
public void demoUpdate(){
Connection con=null;
Statement stmt=null;
try{
//注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获得连接
con= DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest? serverTimezone=UTC", "root", "010126");
//执行SQL语句的对象
stmt=con.createStatement();
//编写SQL语句
String sql2="update user set username='qqq',password='11346',name='网七' where uId=6";
//执行SQL
int i=stmt.executeUpdate(sql2);
if(i>0){
System.out.println("success");
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt=null;
}
if(con!=null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
con=null;
}
}
}
//大家有什么问题可以在博客下方留言
更多推荐
JDBC新手入门教程
发布评论