数据库中的JDBC
public class JDBCDemo {
public static void main(String[] args) {
String url = "jdbc:oracle:thin:@localhost:1521:orcl"; //连接ORL目标地址
String driver = "oracle.jdbc.driver.OracleDriver"; //驱动类名
String userName = "scott"; //用户名
String pwd = "tiger"; //密码
try {
Class.forName(driver); //加载数据库驱动
Connection con = DriverManager.getConnection(url,userName,pwd); //创建数据库连接
String set = "select * from myemp"; //查询表
// String del = "delete from myemp where empno = ?";
// PreparedStatement pre = con.prepareStatement(del);
// int empno = 7876;
// pre.setInt(1, empno);
// pre.executeUpdate(); //更新数据库内容
// String upd = "update myemp set sal = 5000 where empno = ?";
// PreparedStatement pre = con.prepareStatement(upd);
// int empno = 7788;
// pre.setInt(1, empno);
// pre.executeUpdate();
String ins = "insert into myemp (empno,ename,job,sal)values(?,?,?,?)";
PreparedStatement pre = con.prepareStatement(ins);
int empno = 7878;
String ename = "张三";
String job = "清洁工";
int sal = 5000;
pre.setInt(1,empno);
pre.setString(2,ename);
pre.setString(3,job);
pre.setInt(4,sal);
pre.executeUpdate();
Statement stm = con.createStatement();
//通过Conection对象中的crateStatement创建一个Statement对象,通过新建的对象来执行SQL语句
ResultSet res = stm.executeQuery(set); //用ResultSet接受查询结果
while(res.next()){
System.out.print(res.getInt("empno")+"\t");
System.out.print(res.getString("ename")+"\t");
System.out.print(res.getString("job")+"\t");
System.out.println(res.getString("sal"));
}
con.close();
stm.close();
res.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。