在我的代码中,我的最终输出与 MySQl 控制台输出不同:
public class d3 {
Connection con;
String dbName = "mydb";
String dbUsername = "root";
String dbPassword = "2323";
String dbUrl = "jdbc:mysql://localhost/";
public d3() {
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Find database successfuly");
} catch (Exception e) {
System.err.println("Unable to find and load driver");
System.exit(1);
}
}
public void connectToDB() {
try {
con = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
System.out.println("Connect to database succesfully");
} catch (SQLException e) {
System.out.println("Can not connect to database");
System.exit(1);
}
}
public void excuteSQL() {
String sqlString1 = "use mydb";
String sqlString2 = "show tables";
String result;
try {
Statement st1 = con.createStatement();
ResultSet result1 = st1.executeQuery(sqlString1);
while(result1.next()){
result = result1.getString(dbName);
System.out.println(result1);
}
} catch (SQLException sqle) {
System.out.println("Can not excute sql statement");
}
}
public static void main(String[] args) {
d3 ddd = new d3();
ddd.connectToDB();
ddd.excuteSQL();
}
}
我的输出:
Find database successfuly
Connect to database succesfully
Result set representing update count of 0
但是,我想变成这样:
Find database successfuly
Connect to database succesfully
Database changed // this is shown in console
我将逐步比较 mysql 控制台和我的 IDE 控制台的输出,例如,我想在执行“use mydb”语句后看到“数据库已更改”消息。