1.jdbc插入数据
1.加载驱动
2.建立连接
3.获取Statement对象
4.执行sql语句
5.操作结果
6.关闭jdbc的对象
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) throws SQLException {
Connection conn = null;
Statement stmt = null;
//1.加载驱动
try {
Class.forName("oracle.jdbc.OracleDriver");
//2.建立连接
conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","SCOTT","tiger");
//3.获取Statement对象
stmt = conn.createStatement();
//4.执行sql语句
int num = stmt.executeUpdate("insert into stu(id,name,pwd)values(1,'zhangsan','123')");
//5.操作结果
if(num>0) {
System.out.println("插入成功");
}
//6.关闭jdbc的对象
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally {
if(stmt!=null) {
stmt.close();
}
if (conn!=null) {
conn.close();
}
}
}
}
2.jbdc提前预编译插入数据
1.注册驱动
2.建立连接
3.创建执行sql语句的statement对象,提前预编译
4.设定 参数下标从1开始
5.执行sql语句
6.处理结果
7.关闭对象
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Test2 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.注册驱动
Class.forName("oracle.jdbc.OracleDriver");
//2.建立连接
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","SCOTT","tiger");
//3.创建执行sql语句的statement对象
//提前预编译
PreparedStatement pstmt = conn.prepareStatement