driver为JDBC的驱动。
url为数据库的地址。
usrname和password分别为数据库的用户名和密码。
Connection类用来连接MySQL。
ResultSet用来存储结果,一般用到select语句时用。
import java.sql.*;
class MySQL {
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://localhost:3306/school";
private static final String usrname = "root";
private static final String password = "";
private Connection con = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
//创建student表
public MySQL() {
String sql = "create table if not exists student(name char(10), "
+ "sno char(10) primary key, age smallint, sex char(6), "
+ "sdept char(4))";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
//抛出未找到驱动的异常
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url, usrname, password);//连接数据库
ps = con.prepareStatement(sql);//预编译sql语句
ps.executeUpdate();//更新
//关闭相应的对象,注意顺序问题,不可逆。
try {
if (ps != null)
ps.close();
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//插入一条信息
public void addInfo(String name, String sno, int age, String sex,
String sdept) {
String sql = "insert into student(name, sno, age, sex, sdept) "
+ "values(‘" + name + "‘, ‘" + sno + "‘, " + age
+ ", ‘" + sex + "‘, ‘" + sdept + "‘);";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url, usrname, password);
ps = con.prepareStatement(sql);
ps.executeUpdate();
try {
if (ps != null)
ps.close();
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//删除一条信息
public void deleteInfo(String name) {
String sql = "delete from student where name = ‘" + name + "‘;";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url, usrname, password);
ps = con.prepareStatement(sql);
ps.executeUpdate();
try {
if (ps != null)
ps.close();
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
//查询一条信息
public void searchInfo(String name) {
String sql = "select * from student where name like ‘" + name + "‘;";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url, usrname, password);
ps = con.prepareStatement(sql);
rs = ps.executeQuery();//接受查询结果,并存储在rs中。
if (rs.next()) {
do {
System.out.println("Name: " + rs.getString(1) + ", Sno: "
+ rs.getString(2) + ", age:" + rs.getString(3)
+ ", sex: " + rs.getString(4) + ", sdept: "
+ rs.getString(5));
} while (rs.next());
}
else {//如果不存在
System.out.println("There is no one named " + name + ".");
}
try {
if (rs != null)
rs.close();
if (ps != null)
ps.close();
if (con != null)
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class TestJDBC {
public static void main(String[] args) {
System.out.println("Creating table student!");
MySQL sq = new MySQL();
System.out.println("Testing add data!");
sq.addInfo("Mike", "2013210888", 22, "male", "CS");
System.out.println("Testing delete data!");
sq.deleteInfo("Mike");
System.out.println("Testing search data!");
sq.searchInfo("Mike");
System.out.println("Success!");
}
}
时间: 2024-10-25 05:35:42