这是我第一次使用 Eclipse,我试图使用 Java 创建到数据库的连接。我一步一步地完成了这个过程,最后,当我点击运行时,控制台中没有输出。
我究竟做错了什么?
import java.sql.*;
public class jdbcconnection {
public static void main(String[] args) {
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "system", "password");
Statement stm=con.createStatement();
String sql="select * from student";
ResultSet rs=stm.executeQuery(sql);
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getInt(4));
con.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
我的学生表:
SQL> select * from student;
STU_ID STU_NAME BRANC YEAR
---------- ------------ ----- ----------
1 xyz mech 2
2 abc cse 2
3 efg cse 2
您的代码按预期工作;创建和填充您显示的表格并通过 Eclipse 运行您的代码会在控制台中显示这一点:
1 xyz mech 2
2 abc cs 2
3 efg cs 2
因此,您似乎已通过 SQL*Plus 插入了这三行数据,但尚未提交它们。在 Oracle 中,已插入但未提交的数据对任何其他会话都是不可见的。表本身是可见的——因为 DDL 喜欢create table
自动提交,所以你不会得到错误。但表中的数据尚不可见。
只需commit
在插入语句(或退出,默认提交)后在 SQL*Plus 会话中发出 a ,例如:
create table student (stu_id number, stu_name varchar2(10), branch varchar2(5), year number);
insert into student (stu_id, stu_name, branch, year) values (1, 'xyz', 'mech', 2);
insert into student (stu_id, stu_name, branch, year)values (2, 'abc', 'cs', 2);
insert into student (stu_id, stu_name, branch, year)values (3, 'efg', 'cs', 2);
commit;
在你提交你的 Eclipse 会话之后就可以看到数据了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句