我有这个程序:
class DataRetrieve {
DataRetrieve() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/phonebook", "root", "1234");
Statement st = con.createStatement();
st.executeQuery("select * from contacts");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
public class MainProgram {
public static void main(String[] args) {
DataRetrieve dr = new DataRetrieve();
//here i want to print that table rows into Console using this
System.out.println(); // How do you print here that table rows?
}
}
有人可以解释如何在System.out.println
?
答案 0 :(得分:5)
您可以创建ResultSet
。
ResultSet rs = st.executeQuery("select * from contacts");
然后你可以遍历ResultSet,并获取行。
while (rs.next()) {
System.out.println(rs.getString(1)); //gets the first column's rows.
}
获取所有列的数据:
ResultSetMetaData rsmd = rs.getMetaData();
int columnsNumber = rsmd.getColumnCount();
while (rs.next()) {
for(int i = 1; i < columnsNumber; i++)
System.out.print(rs.getString(i) + " ");
System.out.println();
}
如果要打印来自MainProgram
来电的数据库信息,可以返回ResultSet
并在主方法中对其进行迭代。
在这种情况下,您应该在MainProgram
。
class DataRetrieve {
DataRetrieve() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/phonebook", "root", "1234");
Statement st = con.createStatement();
rs = st.executeQuery("select * from contacts");
}
catch(Exception e) {
e.printStackTrace();
}
}
public ResultSet getResultSet() {
return rs;
}
private ResultSet rs = null;
}
在你的主要方法中:
public static void main(String[] args) {
DataRetrieve dr = new DataRetrieve();
//here i want to print that table rows into Console using this
System.out.println(); // How do you print here that table rows?
ResultSet rs = dr.getResultSet();
while (rs.next()) {
System.out.println(rs.getString(1)); //gets the first column's rows.
}
}
答案 1 :(得分:2)
使用以下代码:
class DataRetrieve {
DataRetrieve() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/phonebook", "root", "1234");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from contacts");
System.out.println(rs.getInt(1));
}
catch(Exception e) {
e.printStackTrace();
}
}
}
public class MainProgram {
public static void main(String[] args) {
DataRetrieve dr = new DataRetrieve();
//here i want to print that table rows into Console using this
System.out.println(); // How do you print here that table rows ?
}
}
我已经捕获了ResultSet实例中从数据库返回的行。您可以使用getInt(xxx)
,getString(xxx)
等获取相关值,然后打印它们。
答案 2 :(得分:1)
您所做的一个基本错误是打开连接但从未关闭。这不是一个好的做法,你应该总是关闭它以释放资源。
在您的代码中,首先st.executeQuery("select * from contacts");
将返回一个ResultSet
对象,您需要做的只是迭代ResultSet
并获取行。
DataRetrieve() {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("yourUrl", "userName", "password");
Statement st = con.createStatement();
ResultSet resultSet = st.executeQuery("select * from contacts");
while(resultSet.next())
{
String columnName1= rs.getString("nameOfColumn1");
int columnName2= rs.getInt("nameOfColumn2");
float columnName3= rs.getFloat("nameOfColumn3");
System.out.println(columnName1+"\t"+ columnName2+"\t"+ columnName3);
}
}
}
catch(SQLException e) {
e.printStackTrace();
}
finally{
//dont forget the closing statements
}