//jdbc四要素
//驱动:决定连接的是何种类型的数据库
private static String driver = "oracle.jdbc.OracleDriver";
//url: 决定连接的是哪个主机上的具体的数据库,通常都一样
private static String url = "jdbc:oracle:thin:@localhost:1521:XE";
private static String user="cyg";
private static String password = "cyg
jdbc六大步
Connection conn =null;
PreparedStatement ps = null;
try {
//1.注册驱动
Class.forName(driver);
//建立连接
conn = DriverManager.getConnection(url, user, password);
//3.创建Statement对象
String sql = "insert into test values(my_seq.nextval,?,?)";
ps = conn.prepareStatement(sql);
//4.执行SQL语句
ps.setString(1,name);
ps.setInt(2, age);
//此时一定不要传SQL语句
ps.execute();
//5.操作结果集
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//6.关闭资源
if(ps!=null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
扫描二维码,在手机上阅读!
评论