开发者社区> 问答> 正文

java 连接MYSQL 进行查询怎么修改

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上输出查询到的信息

展开
收起
蛮大人123 2016-03-05 00:14:41 2339 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    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();
            }
        }
    }
    2019-07-17 18:52:48
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载

相关镜像