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) {
//ResultSet rs = null;
Statement stem = null;
Connection conn = null;
//判断是否输入4个参数
if(args.length != 4)
{
System.out.println("Parameter Error! Please Input again");
System.exit(-1);
}
String city_name = args[1];
String provice_id = args[2];
String remark = args[3];
int city_id = 0;
//强制类型转换city_id的值必须输入整数
try {
city_id = Integer.parseInt(args[0]);
}catch (NumberFormatException e){
System.out.println("Parameter is error! please input the number");
System.exit(-1);
}
try{
Class.forName("com.mysql.jdbc.Driver");//注册jdbc驱动
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/dw","root","root");
//建立jdbc的连接url,username,password,
stem = conn.createStatement();//创建一个声明语句
String sql = "insert into code_city values (" + city_id + ",'" + city_name + "'," + provice_id + ",'" + remark +"')";
//String sql2 = "insert into code_city values(8,'梨园',1,'dafsdfafsd')";
//String sql3 = "delete from code_city where city_id = 8";
System.out.println(sql);
stem.executeUpdate(sql);
//stem.executeUpdate(sql2);
//stem.executeUpdate(sql3);
}catch (ClassNotFoundException e){
System.out.println("Driver is error");
e.printStackTrace();
}catch (SQLException e) {
System.out.println("Mysql Statement is error");
e.printStackTrace();
}finally{
try {
if (stem != null)
{
stem.close();
stem =null;
}
if (conn != null)
{
conn.close();
conn = null;
}
}catch (SQLException e){
System.out.println("Mysql is error while close it");
e.printStackTrace();
}
}
}
}
测试DML语言
最新推荐文章于 2023-09-06 09:58:44 发布