标签:
JDBC(Java Data Base Connectivity,java数据库连接),那么我们该如何用java进行数据库的连接呢。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBC {
public static void main(String[] args) {
//1.导入jar包 项目名---->buid path----->Add E~
//2.加载驱动
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//3.创建链接对象
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","xkq","123456");
//4.创建执行对象
PreparedStatement pst =con.prepareStatement("select * from stuInfo");
//5.执行sql语句
ResultSet rs=pst.executeQuery();
//6.获取结果集合
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getInt(3));
System.out.println(rs.getString(4));
System.out.println(rs.getString(5));
System.out.println(rs.getString(6));
System.out.println(rs.getString(7));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
标签:
原文地址:http://www.cnblogs.com/zhaoYuQing-java2015/p/4768129.html