public class JDBCDemo {
private final static String URL = "jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=GBK";
private final static String USERNAME = "root";
private final static String PWD = "123";
public static void update() { // 增删查改
Connection connection = null;
Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
connection = DriverManager.getConnection(URL, USERNAME, PWD);
statement = (Statement) connection.createStatement();
String sql = "insert into StuInfo (name,sex,age,banji) values ('薛宁123','男',23,'高中');";
int count = statement.executeUpdate(sql);
if (count > 0) {
System.out.print("操作成功");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
try {
if(statement!=null) {
statement.close();
}
if(connection!=null) {
connection.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
update();
}
}