package cn.itcas.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Jdbc_Demo2 {
public static void main(String[] args) {
Statement statement=null;
Connection root=null;
try {
Class.forName("com.mysql.jdbc.Driver");//注册驱动
root = DriverManager.getConnection("jdbc:mysql:///db3", "root", "555");//创建访问数据库的IP地址端号和库 输入用户名和密码
statement = root.createStatement();//获取执行sql的对象
String r ="insert into emp values (10,'ww',55)";
int i = statement.executeUpdate("insert into emp values (4,'www',50)");//执行DDL、DML、DQL语句方法
System.out.println(i);//打印结果
if (i>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if (statement!=null){//防止空指针异常
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}if (root!=null){//防止空指针异常
try {
root.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
JDBC insert语句
最新推荐文章于 2024-02-22 14:26:04 发布