Java Database Connectivity
网页后端基础:连接数据库
调用mysql-connector-java包(包装了方法),实现数据库的连接。
MySQL数据库结构
运行结果
源码
package com.company;
import java.sql.*;
public class JDBC {
String getInfo() {
String url = "jdbc:mysql://localhost:3306/test?useTimezone=true&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true";
String username = "root";
String password = "root";
Connection conn = null;
PreparedStatement pst = null;
String sql = "select age,name from user";
ResultSet rs = null;
User user = new User();
String result = "";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
//3.获取用于向数据库发送sql语句的
pst =conn.prepareStatement(sql);//在此次传入,进行预编译
pst.executeQuery();//在这里不需要传入sql
sql = "select age,name from user";
rs = pst.executeQuery(sql);
while(rs.next()) {
user.setAge(rs.getInt("age"));
user.setName(rs.getString("name"));
result += user.toString();
}
}catch (Exception e){
e.printStackTrace();
}
if(rs!=null || pst!=null || conn!=null){
try{
assert rs != null;
rs.close();
pst.close();
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
Result login(int username, int password){
Result result = new Result();
String url = "jdbc:mysql://localhost:3306/test?useTimezone=true&serverTimezone=GMT%2B8&useSSL=false&allowPublicKeyRetrieval=true";
String user = "root";
String pswd = "root";
Connection conn = null;
PreparedStatement pst = null;
String sql = "select username,password from user";
ResultSet rs = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection(url, user, pswd);
pst =conn.prepareStatement(sql);
pst.executeQuery();
sql = "select username,password from user";
rs = pst.executeQuery(sql);
while(rs.next()) {
if(username == rs.getInt("username") &&
password == rs.getInt("password")){
rs.close();
pst.close();
conn.close();
result.setCode(10000);
result.setMsg("登陆成功");
return result;
}
}
}catch (Exception e){
e.printStackTrace();
}
try{
assert rs != null;
rs.close();
pst.close();
conn.close();
}catch (Exception e) {
e.printStackTrace();
}
result.setCode(10001);
result.setMsg("登陆失败");
return result;
}
}
package com.company;
public class Result {
private int code;
private String msg;
public String toString(){
String str;
str = "{ 'code': " + code + ", " +
"'msg': '" + msg + "' " +
"}";
return str;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
package com.company;
public class User {
private int age;
private String name;
public String toString(){
String str;
str = "{ 'age': " + age + ", " +
"'name': '" + name + "' " +
"}";
return str;
}
String getName() {
return name;
}
void setName(String name) {
this.name = name;
}
int getAge() {
return age;
}
void setAge(int age) {
this.age = age;
}
}
最后:主函数调用
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
JDBC jdbc = new JDBC();
String str = jdbc.getInfo();
System.out.println(str);
int username = in.nextInt(), password = in.nextInt();
Result result = jdbc.login(username, password);
System.out.println(result.toString());
}
}