如何将表数据从数据库打印到 Java 控制台?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17739233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I print table data from database to Java Console?
提问by Hiroga Katageri
I'm not good with Java I've only learned the basics and it's still kind of fresh somehow. I want to know how does someone print a row from a table to the Java Console.
我不擅长 Java 我只学习了基础知识,不知何故它仍然有点新鲜。我想知道某人如何将表中的一行打印到 Java 控制台。
try {
statement = con.prepareStatement(
"SELECT * FROM users WHERE Username ='" +
LOGIN_UsernameField.getText() + "' AND Password ='" +
LOGIN_PasswordField.getPassword() + "'");
System.out.println ();
}
That system.out.println
thing I know that doesn't work of course, I need something to replace it to print out the rows that comes from the SQL Statement right above it.
这system.out.println
我知道,没有当然的工作的事情,我需要的东西来取代它打印出来自它上面的SQL语句正确的行。
回答by Richie
Try this tutorialfrom Oracle. It is simple and straight forward.
It would be something like this
它会是这样的
try{
statement = con.prepareStatement(
"SELECT * FROM users WHERE Username = ? AND Password =?");
statement.setString(0,LOGIN_UsernameField.getText());
statement.setString(0,LOGIN_PasswordField.getPassword());
ResultSet rs = statement.execute();
while(rs.next()){
System.out.println("UserName:"+rs.getString("Username")+"\nPassword:"+rs.getString("Password"));
}
}catch(){
//handle exceptions
}finally{
//close statements, statement and resultset here..
}
回答by Aniket Thakur
Here is the code
这是代码
System.out.println("-------- MySQL JDBC Connection Testing ------------");
Connection connection = null;
try {
connection = DriverManager
.getConnection("jdbc:mysql://localhost:3306/testDb", "userName", "Password");
} catch (SQLException e) {
for(Throwable ex : e) {
System.err.println("Error occurred " + ex);
}
e.printStackTrace();
}
if (connection != null) {
System.out.println("Connected to database!");
} else {
System.out.println("Failed to make connection!");
}
try {
Statement stmt = connection.createStatement();
String query = "select * from person ;";
//person is the table name
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String name = rs.getObject(1).toString();
String gender = rs.getObject(2).toString();
System.out.println("Name of the person is " + name + " and his gender is " + gender);
//Person table has name and gender column
}
} catch (SQLException e) {
e.printStackTrace();
for(Throwable ex : e) {
System.err.println("Error occurred " + ex);
}
System.out.println("Error in fetching data");
}
Note : I am using JDBC 4
that come with Java 7
and hence no need to explicitly provide driver. Compiler will take it automatically from the class path. You can download MySqlConnector.jar and put it in your classpath.If you are not using java 7 you will have to explicitly load the driver using Class.forName()
. And of course I am using MySql... the driver will change with the database you use.
注意:我使用的JDBC 4
是随附的Java 7
,因此无需明确提供驱动程序。编译器会自动从类路径中获取它。您可以下载 MySqlConnector.jar 并将其放在您的类路径中。如果您不使用 java 7,则必须使用Class.forName()
. 当然,我使用的是 MySql……驱动程序会随着您使用的数据库而变化。
Hope this helps.
希望这可以帮助。