学期末233333
(1)java.sql.Connection: 负责连接数据库。
(2)java.sql.Statement: 负责执行数据库SQL语句。
(3)java.sql.ResultSet: 负责存放查询结果。
添加数据:
public class Insert {
public static void main(String args[]) throws Exception {
//数据库数据乱码处理
String url = "jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=GBK";
String user = "root";
String password = "123456";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, user, password);
//添加数据(1)
Statement stat = conn.createStatement();
String sql = "insert into t_book(bookid, bookname, bookprice) values('0015','数据库原理',32)";
int i = stat.executeUpdate(sql);
System.out.println("添加"+i+"行数据");
stat.close();
//添加数据(2)
sql = "insert into t_book(bookid,bookname,bookprice) values(?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "0015");
ps.setString(2, "JavaEE");
ps.setInt(3, 233);
i = ps.executeUpdate();
System.out.println("添加"+i+"行数据");
ps.close();
conn.close();
}
}
删除数据:
public class Delete {
public static void main(String args[]) throws Exception {
String url = "jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=GBK";
String user = "root";
String password = "123456";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url,user,password);
//删除数据(1)
Statement stat = conn.createStatement();
String sql = "delete from t_book where bookid = '0015'";
int i = stat.executeUpdate(sql);
System.out.println("删除"+i+"行数据");
stat.close();
//删除数据(2)
sql = "delete from t_book where bookid = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "0015");
i = ps.executeUpdate();
System.out.println("删除"+i+"行数据");
ps.close();
conn.close();
}
}
更新数据:
public class Update {
public static void main(String args[]) throws Exception {
String url = "jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=GBK";
String user = "root";
String password = "123456";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url,user,password);
//更新数据(1)
Statement stat = conn.createStatement();
String sql = "update t_book set bookprice = 233 where bookid = 0002";
int i = stat.executeUpdate(sql);
System.out.println("成功更新"+i+"行数据");
stat.close();
//更新数据(2)
sql = "update t_book set bookprice = ? where bookid = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, 332);
ps.setString(2, "0002");
i = ps.executeUpdate();
System.out.println("成功更新"+i+"行数据");
ps.close();
conn.close();
}
}
查询数据:
public class Select {
public static void main(String args[]) throws Exception{
String url = "jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=GBK";
String user = "root";
String password = "123456";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url,user,password);
//查询数据(1)
Statement stat = conn.createStatement();
String sql = "select * from t_book";
ResultSet rs = stat.executeQuery(sql);
stat.close();
//查询数据(2)
sql = "select * from t_book where bookid = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, "0002");
rs = ps.executeQuery();
while(rs.next()){
String bookid = rs.getString("bookid");
String bookname = rs.getString("bookname");
int bookprice = rs.getInt("bookprice");
System.out.println(bookid+" "+bookname+" "+bookprice);
}
ps.close();
rs.close();
conn.close();
}
}