import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDML2 {
public static void main(String[] args) {
Connection conn = null;
Statement stat = null;
int id = 0;
String name = args[1];
if (args.length != 2) {
System.out.println("Parameter Error! Please Input Again!");
System.exit(-1);
}
id = Integer.parseInt(args[0]);
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/student?user=root&password=root");
stat = conn.createStatement();
String sql = "insert into c values(" + id +",'" + name +"')";
System.out.println(sql);
stat.executeUpdate(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try{
if(null != stat){
stat.close();
stat = null;
}
if(null != conn) {
conn.close();
conn = null;
}
} catch(SQLException e) {
e.printStackTrace();
}
}
}
}
DML插入一条数据
最新推荐文章于 2024-05-27 23:58:35 发布