我写了我的第一个程序,但在输出中我看不到任何东西。我在mysql中创建我的表。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class tester {
public tester() {
}
public static void main(String[] args) {
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "123456";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
ResultSet rs;
try {
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(
"jdbc:mysql://./test", user, pass);
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT * FROM student");
rs = statement.getResultSet();
while (rs.next()) {
System.out.print(rs.getString("sname")+("\t")) ;
System.out.print(rs.getString("sfamily")+("\t")) ;
System.out.print(rs.getString("saddress")+("\t")) ;
}
rs.close();
statement.close();
connection.close();
} catch (Exception e) {
}
}
}
在我的输出中我有: 跑: 建立成功(总时间:0秒) 我看不到我的记录。我该怎么办? 我在我的项目中添加了mysql-connector-java-5.1.18。
答案 0 :(得分:1)
首先,你没有得到任何例外,因为你没有打印它们。改变这一部分:
} catch (Exception e) {
}
到此:
} catch (Exception e) {
e.printStackTrace();
}
我怀疑这里的连接字符串会出现问题:
connection = DriverManager.getConnection("jdbc:mysql://./test", user, pass);
答案 1 :(得分:0)
请尝试使用以下代码
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",user,pass);
statement = connection.createStatement();
resultSet = statement.executeQuery("SELECT * FROM student");
while (resultSet.next()) {
System.out.print(resultSet.getString("sname")+("\t")) ;
System.out.print(resultSet.getString("sfamily")+("\t")) ;
System.out.print(resultSet.getString("saddress")+("\t")) ;
}
并添加此功能以阻止
catch (Exception e) {
e.printStackTrace();
}
这就是为什么你没有得到任何例外。
<强>建议强>
您不需要两个不同的结果集。你也可以这样做:
resultSet = statement.executeQuery("SELECT * FROM student");
while (resultSet.next()) {
System.out.print(resultSet.getString("sname")+("\t")) ;
System.out.print(resultSet.getString("sfamily")+("\t")) ;
System.out.print(resultSet.getString("saddress")+("\t")) ;
}
关闭finally
区块中的连接,结果集等。
根据Java命名约定,您的班级名称应为Tester
。
答案 2 :(得分:0)
您使用的是像eclipse这样的IDE吗?否则,编译并运行程序如下:
javac tester.java
java tester
在你的catch子句中执行以下操作,而不是完全忽略异常:
catch (Exception e){
e.printStackTrace();
}
答案 3 :(得分:0)
public static void main(String[] args) {
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
try {
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(
"jdbc:mysql://localhost/ums1", user, pass);
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM user");
while (rs.next()) {
System.out.print(rs.getString(1) + ("\t"));
System.out.print(rs.getString(2) + ("\t"));
System.out.print(rs.getString(3) + ("\t"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
connection.close();
rs.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
介意更改数据库URL和表名。我用我的一个测试过。
答案 4 :(得分:0)
1.您不需要两个结果集。您可以使用以下代码。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
//</editor-fold>
/**
*
* @author mohsen
*/
public class tester {
public tester() {
}
public static void main(String[] args) {
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "123456";
String dbUrl ="jdbc:mysql://localhost:3306/test1";
Connection connection = null;
Statement statement = null;
/* ResultSet resultSet = null; */
ResultSet rs =null;
try {
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(dbUrl, user, pass);
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM student");
// rs = statement.getResultSet();
while (rs.next()) {
System.out.print(rs.getString("sname")+("\t")) ;
System.out.print(rs.getString("sfamily")+("\t")) ;
System.out.print(rs.getString("saddress")+("\t")) ;
}
rs.close();
statement.close();
connection.close();
} catch (Exception e) {
}
}
}
2.尝试打印Exception。所以你得到的是错误。
3.最后终于紧密联系。