实验五 通用接口数据库编程
制作不易!点个关注,希望给大家带来价值
第1关 JDBC 查询数据
package step1;
import java.sql.*;
public class Query {
public static void queryDB() {
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
String username = "root";
String password = "123123";
String url = "jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
String sql="select * from s";
stat = conn.createStatement();
rs = stat.executeQuery(sql);
System.out.println("sno sname status city");
while (rs.next()){
System.out.print(rs.getString("sno")+" ");
System.out.print(rs.getString("sname")+" ");
System.out.print(rs.getString("status")+" ");
System.out.println(rs.getString("city"));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stat != null) {
stat.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
第2关 JDBC 插入数据
package step1;
import java.sql.*;
public class Insert {
public static void insertDB() {
Connection conn = null;
Statement stat = null;
String username = "root";
String password = "123123";
String url = "jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
stat=conn.createStatement();
String sql = "INSERT INTO s VALUES('S6','泰鑫',10,'十堰')";
int n=stat.executeUpdate(sql);
if(n>0){
System.out.println("插入成功!");
}else{
System.out.println("插入失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (stat != null) {
stat.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
第3关 JDBC 更新数据
package step1;
import java.sql.*;
public class Update {
public static void updateDB() {
Connection conn = null;
Statement stat = null;
String username = "root";
String password = "123123";
String url = "jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
String sql="UPDATE s SET sname='东风',status='40',city='十堰' WHERE sno='S5'";
stat=conn.createStatement();
int n=stat.executeUpdate(sql);
if(n>0){
System.out.println("更新成功!");
}else{
System.out.println("更新失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (stat != null) {
stat.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
第4关 JDBC 删除数据
package step1;
import java.sql.*;
public class Delete {
public static void deleteDB() {
Connection conn = null;
Statement stat = null;
String username = "root";
String password = "123123";
String url = "jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
String sql="delete from s where sno='S5'";
stat=conn.createStatement();
int n = stat.executeUpdate(sql);
if(n>0){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (stat != null) {
stat.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
第5关 JDBC 条件查询
package step1;
import java.sql.*;
import java.util.Scanner;
public class QuerySno {
public static void queryDB() {
Scanner scanner = new Scanner(System.in);
String sno = scanner.next();
Connection conn = null;
PreparedStatement psmt = null;
ResultSet rs = null;
String username = "root";
String password = "123123";
String url = "jdbc:mysql://127.0.0.1:3306/demo?useUnicode=true&characterEncoding=utf-8";
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
String sql="select * from s where sno=?";
psmt = conn.prepareStatement(sql);
psmt.setString(1,sno);
System.out.println("sno\tsname\tstatus\tcity");
rs=psmt.executeQuery();
while(rs.next()){
System.out.print(rs.getString("sno")+"\t");
System.out.print(rs.getString("sname")+"\t");
System.out.print(rs.getInt("status")+"\t");
System.out.print(rs.getString("city"));
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (psmt != null) {
psmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}