importjava.security.interfaces.RSAKey;import oracle.jdbc.*;import java.sql.*;public classTestOracle {/***@paramargs*/
public static voidmain(String[] args) {//载入驱动
try{
Class.forName("oracle.jdbc.OracleDriver");//以下url的获取:window--Open Perspective-->MyEclipse DataBase Browser--new--driver templete选择thin ,//username,pass,jar包等这些都写一下、导入一下。然后测试一下,如果提示成功,就可以复制那个URL//url:jdbc:oracle:thin:@[:<1521>]:
String url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";//数据库连接地址
String user="root"; //数据库用户名
String pass="111"; //密码//通过驱动管理器获取oracle数据库连接
Connection conn=DriverManager.getConnection(url,user,pass);//添加记录
Statement st=conn.createStatement();//添加语句//String sql1="insert into student values(1,'aaa','123')";
String sql2="insert into student values(2,'bbb','0107')";//修改语句
String sql3="update student set pass='99912' where sid=1";//删除语句//String sql="delete from sudent where sid=1";//执行语句//st.execute(sql1);//st.execute(sql3);//查询语句(读取所有记录)//String sql="select * from student order by sid ";//String sql="select * from student where name='aaa' and pass='111' order by sid ";//模糊查询//String sql="select * from student where name like 'a%' ";//'a%':以a开头。'_a%'//ResultSet rs = st.executeQuery(sql);//遍历结果输出记录://判断是否有下一条记录存在,如果存在则游标往后移动一格//while (rs.next()) {//System.out.println(rs.getString("sid")+";"+rs.getString("name")+";"+rs.getString("pass"));// //System.out.println(rs.getString("num"));//}//预编译:记录插入
String sql="insert into student values(?,?,?)";//?进行占位,后预编译。
PreparedStatement ps=conn.prepareStatement(sql);//输入参数
ps.setInt(1, 6);//第一个是参数索引,第二个是值。
ps.setString(2, "sss");
ps.setString(3,"18");if(ps.execute()) {
System.out.println("记录添加成功");
}//预编译:修改记录//String sql="update student set pass=? where name=?";//PreparedStatement ps=conn.prepareStatement(sql);//ps.setString(1, "666");//ps.setString(2, "aaa");//
//ps.execute();//
//预编译:删除记录//String sql="delete from student where sid=?";//PreparedStatement ps=conn.prepareStatement(sql);//ps.setIn(1, 1);//
//ps.execute();// //预编译:查找//String sql="select * from student where name like ? ";//这里只能是问号,问号不能放在字符串里面//PreparedStatement ps=conn.prepareStatement(sql);//ps.setString(1, "%a%");//模糊查询条件在这里给出//
//rs=ps.executeQuery();//
// //输出结果//while (rs.next()) {//System.out.println(rs.getString("sid")+";"+rs.getString("name")+";"+rs.getString("pass"));//}//System.out.println("数据库连接成功");
System.out.println("记录添加成功");
}catch(ClassNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如果觉得《java oracle连接数据库_Java 连接 Oracle 数据库》对你有帮助,请点赞、收藏,并留下你的观点哦!