public class test4{
public static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
public static final String DBURL = "jdbc:mysql://localhost:3306/ace" ;
public static final String DBUSER = "root" ;
public static final String DBPASS = "root" ;
public static void main(String args[]) throws Exception {
Connection conn = null ;
Statement stmt = null ;
Class.forName(DBDRIVER) ;
String sql = "SELECT user_name FROM users;";
conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
stmt = conn.createStatement() ;
stmt.executeUpdate(sql) ;
stmt.close() ;
conn.close() ;
}
}
请问这样怎么修改才能正常的进行查询 和在java上输出查询到的信息
public class DataBase {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
public DataBase() {
// oracle 的jdbc 连接
try {
OracleDataSource ds = new OracleDataSource();
ds.setUser("uamqas02");
ds.setPassword("uamqas02");
ds.setURL("jdbc:oracle:thin:@localhost:1521:devdb01");
conn = ds.getConnection();
stmt = conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ResultSet executeQuery(String sql) {
rs = null;
try {
// stmt=conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
System.err.println("executeQuerty:" + e.getMessage());
}
return rs;
}
// ---------------------------数据库的更新、修改、删除操作-------------------------------------------------
public boolean executeUpdate(String sql) // 更新方法(增、删、改)
{
boolean temp = false;
try {
stmt = conn.createStatement();
int i = stmt.executeUpdate(sql);
if (i >= 1)
temp = true;
} catch (SQLException e) {
e.printStackTrace();
System.err.println("executeUpdate:" + e.getMessage());
}
return temp;
}
public boolean saveOrUpdateCommit(String sql, String sql2) {
boolean temp = true;
try {
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.addBatch(sql);
stmt.addBatch(sql2);
stmt.executeBatch();
conn.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
temp = false;
// 回滚
try {
conn.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
conn.commit();
} catch (SQLException e) {
// TODO Auto-generated catch block
temp = false;
e.printStackTrace();
}
return temp;
}
public void closeStmt() {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void closeConn() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。