Skip to content

Commit

Permalink
feat(*): 更新第十四周
Browse files Browse the repository at this point in the history
  • Loading branch information
LeslieLeung committed Dec 7, 2020
1 parent 9816442 commit 6fa8136
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
Binary file added lib/mysql-connector-java-8.0.13.jar
Binary file not shown.
58 changes: 58 additions & 0 deletions src/chapter14/DBOperate1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package chapter14;

import java.sql.*;

/**
* description:
* author: Leslie Leung
* date: 2020/12/7
*/
public class DBOperate1 {
public static void main(String[] args) throws SQLException {
Connection connection = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3307/STUDENTDB2?characterEncoding=utf8&useSSL=false&serverTimezone=Hongkong", "root", "password");

String sql = "select NO,NAME,AGE,CLASS from students where name like ? and age=?";

PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setObject(1, "小%");
stmt.setObject(2,23);
ResultSet rs = stmt.executeQuery();
while (rs.next())
{
System.out.print(rs.getString(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getInt(3)+"\t");
System.out.print(rs.getString(4)+"\n");
}
System.out.println("------------------------------------");

String sql1 = "insert into STUDENTS(NO,NAME,AGE,CLASS) values(?,?,?,?)";
stmt = connection.prepareStatement(sql1);
stmt.setObject(1,"2018100xxxx");
stmt.setObject(2,"xxx");
stmt.setObject(3, 21);
stmt.setObject(4,"软件工程1801");

stmt.executeUpdate();
sql = "select NO,NAME,AGE,CLASS from STUDENTS ";
stmt = connection.prepareStatement(sql);
rs = stmt.executeQuery();

while (rs.next())
{
System.out.print(rs.getString(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 {
connection.close();
}
}
}

0 comments on commit 6fa8136

Please sign in to comment.