myeclipse连接数据库oracle

时间:2023-03-08 22:28:09
 package xsl;

 import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet; public class Test {
public static void main(String[] args) throws Exception {
//加载数据包
Class.forName("oracle.jdbc.driver.OracleDriver");
//开始连接
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl","xsl","xsl");
//准备sql语句
PreparedStatement pstmt = conn.prepareStatement("select * from student");
//执行sql语句
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
System.out.println(rs.getString(1)+"\t"+rs.getString(2));
}
rs.close();
pstmt.close();
// PreparedStatement pstmt = conn.prepareStatement("insert into student(sname,sid) values ('xxx',2013550400)");
// pstmt.executeUpdate();
// pstmt.close();
conn.close(); } }