JDBC的定义
JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序,同时,JDBC也是个商标名。
JDBC的使用
1,常用JDBC示例,输出学生表的全部内容
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//java10是数据库的名字
String url="jdbc:mysql://localhost:3306/java10?useSSL=false&serverTimezone=UTC";
//登录数据库用户名
String username="root";
//登录数据库密码
String pwd="Java20190713*yy";
Connection conn = DriverManager.getConnection(url,username,pwd);
//createStatement()创建一个 Statement 对象来将 SQL 语句发送到数据库。
Statement st=conn.createStatement();
//从数据库里读取的内容,返回一个结果值
ResultSet rs=st.executeQuery("select name,id,score,subject from student");
//只要 结果集合里,至少有一条记录,就会输出
while(rs.next()){
System.out.println("名字"+rs.getString("name"));
System.out.println("分数"+rs.getString("score"));
}
//从下到上关闭
rs.close();
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2,优化
每次使用JDBC时,都要连接数据库,故把它写成一个对象
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCComon {
public Connection getConnection() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.cj.jdbc.Driver");
//java10是数据库的名字
String url="jdbc:mysql://localhost:3306/java10?useSSL=false&serverTimezone=UTC";
//登录数据库用户名
String username="root";
//登录数据库密码
String pwd="Java20190713*yy";
Connection conn = DriverManager.getConnection(url,username,pwd);
return conn;
}
}
用JDBC实现增删改查
第一步
新建一个student对象
第二步
编写实现增删改查代码
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCTest2 {
JDBCComon jdbcc=new JDBCComon();
public static void main(String[] args) {
// TODO Auto-generated method stub
JDBCTest2 jdbc=new JDBCTest2();
//增
/**
Student student=new Student();
student.setName("JDBC");
student.setScore(100);
student.setSubject("数学");
student.setTeam_id(1);
jdbc.insert(student);
**/
//删
// jdbc.delete("张三");
//改
jdbc.update("李四", 59);
}
//增
public boolean insert(Student s){
boolean f=false;
try {
Connection conn = jdbcc.getConnection();
Statement st=conn.createStatement();
String sql="insert into student (name,score,subject,team_id) values('"+s.getName()+"',"+s.getScore()+",'"+s.getSubject()+"',"+s.getTeam_id()+")";
int rs=st.executeUpdate(sql);
if(rs>0){
f=true;
}
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return f;
}
//删
public boolean delete(String name){
boolean f=false;
try {
Connection conn = jdbcc.getConnection();
Statement st=conn.createStatement();
String sql="delete from student where name='"+name+"'";
int rs=st.executeUpdate(sql);
if(rs>0){
f=true;
}
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return f;
}
//改
//根据名字,修改分数
public boolean update (String name,double score){
boolean f=false;
try {
Connection conn = jdbcc.getConnection();
Statement st=conn.createStatement();
String sql="update student set score="+score+" where name='"+name+"'";
int rs=st.executeUpdate(sql);
if(rs>0){
f=true;
}
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return f;
}
}
可以在数据库中看到更改完成后的数据
用JDBC实现登录与注册
登陆方法
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class UserService {
JDBCComon jdbc=new JDBCComon();
//登录
public boolean login(String name,String pwd){
boolean rs=false;
try {
Connection conn=jdbc.getConnection();
Statement st=conn.createStatement();
String sql="select * from user where name='"+name+"' and pwd='"+pwd+"'";
System.out.println("sql--->"+sql);
ResultSet rss=st.executeQuery(sql);
//rss.next()只要 结果集合里,至少有一条记录,next方法就会返回true
if(rss.next()){
rs=true;
}
//从下往上关
rss.close();
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
}
测试
// TODO Auto-generated method stub
UserService us=new UserService();
if(us.login("小明", "123")){
System.out.println("登录成功");
}else{
System.out.println("登录失败,用户名或者密码错误");
}
}
}
注册方法
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCTest3 {
JDBCComon jdbcc=new JDBCComon();
public static void main(String[] args) {
// TODO Auto-generated method stub
JDBCTest3 jdbc=new JDBCTest3();
User user1=new User();
user1.setName("小sb");
user1.setPwd("123456");
jdbc.insert(user1);
}
public boolean insert(User s1){
boolean f=false;
try {
Connection conn = jdbcc.getConnection();
Statement st=conn.createStatement();
String sql="insert into user (name,pwd) values('"+s1.getName()+"',"+s1.getPwd()+")";
int rs=st.executeUpdate(sql);
if(rs>0){
f=true;
}
st.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return f;
}