package com.oracle.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class acc {
public static void main(String[] args) throws ClassNotFoundException,SQLException{
// TODO Auto-generated method stub
Class.forName("oracle.jdbc.driver.OracleDriver");
String url="jdbc:oracle:thin:@localhost:1521:xe";
String user="Tan";
String password="123456";
Connection conn = DriverManager.getConnection(url, user, password);
System.out.println(conn);
}
}
空指针异常了呀,应该是jdbc连接数据库失败导致的吧,看下15行哪里为空了
看下 driverName 跟oracle版本,jar包是否对应 oracle9之后是使用下边的
driverName = "oracle.jdbc.OracleDriver";
这是我这边写的一个测试jdbc的你可以试下 jar是 ojdbc6-11.2.0.4.0.jar
public class JdbcUtil {
private static String url = "jdbc:oracle:thin:@10.10.10.101:1521:test";
private static String userName = "test";
private static String pwd = "test";
private static String driverName = "oracle.jdbc.OracleDriver";
static {
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws SQLException {
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
String sql = " select b.usercode usercode from loginUser b where b.usercode= ? and b.password=?";
try {
connection = DriverManager.getConnection(url, userName, pwd);
} catch (SQLException e1) {
e1.printStackTrace();
}
try {
ps = connection.prepareStatement(sql);
ps.setString(1, "111111");
ps.setString(2, "123456");
rs = ps.executeQuery();
while (rs.next()) {
String usercode = rs.getString("usercode");
System.out.println("登录用户为:" + usercode);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (ps != null) {
ps.close();
}
if (connection != null) {
connection.close();
}
}
}
}