日期:2014-05-17 浏览次数:20746 次
package jw;
import java.sql.*;
public class Jw {
public static void main(String[] args) {
String url = "jdbc:oracle:thin:@host:1521:test1"; //test为自己数据库的名字,1521为端口
String username = "user";
String password = "pass";
String driver = "oracle.jdbc.driver.OracleDriver";
try {
Class.forName(driver); //加载驱动
Connection conn = DriverManager.getConnection(url, username,
password); //连接数据库
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM JW.XJ_XJB"); // 预处理命令,比Statement 好一些
ResultSet rs = pstmt.executeQuery();
while(rs.next())
{
System.out.println(rs.getString(1));
}
pstmt.close(); //关闭连接
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("-------- MySQL JDBC Connection Testing ------------");
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
System.out.println("MySQL JDBC Driver Registered!");
String myurl = "jdbc:mysql://localhost:3306/test";
String user = "user";
String pwd = "pass";
Connection connection = null;
String sql = null;
Statement stmt = null;
//ResultSet rs = null;
try {
connection = DriverManager
.getConnection(myurl,user,pwd);
stmt = connection.createStatement();
//我要在这里获取上面Resul