还有点问题这个是调好的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 回答
HUX布斯
TA贡献1876条经验 获得超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)); } }
添加回答
举报
0/150
提交
取消