jdbc - java连接数据库进行查询优化跑不通谁能帮我调下通
黄舟
黄舟 2017-04-17 10:57:32
0
1
190

我已经把你昨天给我的那个代码调通了但是另一般还有点问题这个是调好的

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[]

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all (1)
阿神

你的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)); } }
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!