亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java連接數據庫進行查詢優化跑不通誰能幫我調下通

java連接數據庫進行查詢優化跑不通誰能幫我調下通

LEATH 2019-03-14 18:15:37
我已經把你昨天給我的那個代碼調通了但是另一般還有點問題這個是調好的import java.sql.*; public class jdbcyouhuo1 {     public String[] getUserName(String connString, String SQLString) throws SQLException {                  Connection connection = DriverManager.getConnection(connString,"root", "root");        try {                        Statement statement = connection.createStatement();            ResultSet rs = statement.executeQuery(SQLString);            int rowcount = 0;            int i = 0;             if( rs.last()) {                 rowcount = rs.getRow();                                  rs.beforeFirst();            }                       String[] retArray = new String[rowcount];            while (rs.next()) {                retArray[i++] = rs.getString("user_name");                            }            statement.close();            return retArray;         } catch (SQLException e ) {                                    System.err.print("SQLException: ");        } finally {                 }        return null;    }           } 這個是還有問題的一半import java.sql.*;public class jdbcyouhua2 {    public static void main(String[] agrs){         String connString = "jdbc:mysql://localhost/ace?useUnicode=true&characterEncoding=utf-8";        String SQLString = "SELECT user_name FROM users";        jdbcyouhuo1 jb1 = new jdbcyouhuo1();                try{                        String[] rs = jb1.getUserName(connString, SQLString);                                //while(rs.next()){                  System.out.println(rs.getString("user_name"));    }         } catch(Exception e) {             e.printStackTrace();         } finally {                   }    }}是輸出那里還有問題Cannot invoke getString(String) on the array type String[]
查看完整描述

1 回答

?
阿晨1998

TA貢獻2037條經驗 獲得超6個贊

你的Connection變量應該在類之中,在使用ResultSet之前不可以把Statement和Connnection不應該關,你的ResultSet不應被返回而是直接把結果拿到 []string 內再返回


import java.util.Arrays;

import java.lang.*;

import java.sql.*;


public class DatabaseConnectorTest {

        private Connection connection = null;

        private Statement statement = null;


        public void connect(String connString) throws SQLException {

                try {

                        Class.forName("org.sqlite.JDBC");

                        connection = DriverManager.getConnection(connString,"root", "root");

                } catch (SQLException e) {

                        System.err.print("connect SQLException: ");

                        System.err.println(e.getMessage());

                } catch (ClassNotFoundException e) {

                        System.err.print("ClassNotFoundException: ");

                        System.err.println(e.getMessage());

                }

        }

        public String[] getUserName(String connString, String SQLString) {

                ResultSet rs = null;

                try {

                        if(null == connection)

                                connect(connString);

                        if(null == statement)

                                statement = connection.createStatement();

                        rs = statement.executeQuery(SQLString);

                        int rowcount = 0;

                        int i = 1;

                        while(rs.next()) {

                                rowcount = i++;

                        }

                        String[] retArray = new String[rowcount];

                        i=0;

                        rs = statement.executeQuery(SQLString);

                        while (rs.next()) {

                                retArray[i++] = rs.getString("user_name");

                        }

                        return retArray;

                } catch (SQLException e ) {

                        System.err.print("getUserName SQLException: ");

                        System.err.println(e.getMessage());

                } finally {

                        if (statement != null) {

                                try { statement.close(); }

                                catch(SQLException e) {

                                        System.err.print("getUserName Final SQLException: ");

                                        System.err.println(e.getMessage());

                                }

                        }

                }

                return null;

        }

        public static void main(String[] agrs){

                String connString = "jdbc:sqlite:/tmp/test.db"; <<<< 用你自己的

                String SQLString = "SELECT user_name FROM users";

                DatabaseConnectorTest db = new DatabaseConnectorTest();

                String[] user_name = db.getUserName(connString, SQLString);

                System.out.println(Arrays.toString(user_name));

        }

}


查看完整回答
反對 回復 2019-04-15
  • 1 回答
  • 0 關注
  • 334 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號