Test
package sx20191216;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class Test {
public static void main(String[] args) throws Exception {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/hospital";
String user = "root";
String password = "root";
try {
// 加载驱动程序
Class.forName(driver);
// 获得数据库连接
con = DriverManager.getConnection(url, user, password);
// 操作数据库,实现增删改查
String sql = "SELECT * FROM Doctor";
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
// 如果有数据,rs.next()返回true
System.out.print("编号t姓名t年龄t专业n");
while (rs.next()) {
System.out.print(rs.getInt(1) "t");
System.out.print(rs.getString(2) "t");
System.out.print(rs.getInt(3) "t");
System.out.print(rs.getString(4) "n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
}
}
}
Test1
package sx20191216;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
public class Test1 {
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入名字:");
String name1 = scanner.next();
System.out.println("请输入年龄");
int age1 = scanner.nextInt();
System.out.println("请输入专业");
String major1 = scanner.next();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/hospital";
String user = "root";
String password = "root";
try {
// 加载驱动程序
Class.forName(driver);
// 获得数据库连接
con = DriverManager.getConnection(url, user, password);
// 操作数据库,实现插入
String sql = "insert into Doctor values(null,?,?,?)";
ps = con.prepareStatement(sql);
ps.setString(1, name1);
ps.setInt(2, age1);
ps.setString(3, major1);
int result = ps.executeUpdate();
if (result == 1) {
System.out.println("插入成功");
} else {
System.out.println("插入失败");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
}
}
}
Test2
package sx20191216;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
public class Test2 {
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入序号");
int id1 = scanner.nextInt();
System.out.println("请输入名字:");
String name1 = scanner.next();
System.out.println("请输入年龄");
int age1 = scanner.nextInt();
System.out.println("请输入专业");
String major1 = scanner.next();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/hospital";
String user = "root";
String password = "root";
try {
// 加载驱动程序
Class.forName(driver);
// 获得数据库连接
con = DriverManager.getConnection(url, user, password);
// 操作数据库,实现更新
String sql = "UPDATE doctor SET name=?, age = ?,major = ? where id=?";
ps = con.prepareStatement(sql);
ps.setString(1, name1);
ps.setInt(2, age1);
ps.setString(3, major1);
ps.setInt(4, id1);
int result = ps.executeUpdate();
if (result == 1) {
System.out.println("修改成功1");
} else {
System.out.println("修改失败0");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
}
}
}
Test3
package sx20191216;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
public class Test3 {
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入序号");
int id1 = scanner.nextInt();
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/hospital";
String user = "root";
String password = "root";
try {
// 加载驱动程序
Class.forName(driver);
// 获得数据库连接
con = DriverManager.getConnection(url, user, password);
// 操作数据库,实现删除
String sql = "DELETE FROM doctor where id = ? ";
ps = con.prepareStatement(sql);
ps.setInt(1, id1);
int result = ps.executeUpdate();
if (result == 1) {
System.out.println("删除成功1");
} else {
System.out.println("删除失败0");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
}
}
}