public static void main(String[] args){
Connection connection = null;
Statement statement = null;
try{
Class.forName ("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql:///class","root","123456");
String sql = "create table students(id int primary key auto_increment,name String)";
if(connection == null)
return;
statement = connection.createStatement();
int row = statement.executeUpdate(sql);
System.out.println(row);
}
catch(ClassNotFoundException | SQLException e){
throw new RuntimeException(e);
}
finally{
try {
if(statement != null){
statement.close();
}
}
catch (SQLException e) {
e.printStackTrace();
}
try {
if(connection != null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
JDBC代码练习
于 2023-11-01 16:28:28 首次发布