我正在處理一個項目,並從打印數據庫中選擇的數據時遇到一些問題。 這是MySQL連接:如何正確選擇mysql數據庫中的數據? ResultSet錯誤
import java.sql.*;
public class MysqlConnect {
public Connection conn = null;
public String url = "jdbc:mysql://localhost:3306/";
public String dbName = "jdbctutorial";
public String driver = "com.mysql.jdbc.Driver";
public String userName = "birthday";
public String password = "123456";
public Statement stmt = null;
public String query = "";
public int rs = 0;
public PreparedStatement pstmt = null;
public ResultSet rrs = null;
public void crearedatabase() {
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url + dbName, userName, password);
// System.out.println("Connected to the database");
stmt = conn.createStatement();
} catch (Exception e) {
System.out.println("Baza de date nu a fost creata");
}
}
public void executareSql() {
try {
rs = stmt.executeUpdate(query);
} catch (Exception e) {
System.out.println("Eroare sql");
}
}
public void executaresSqlPrepareSt(String nume, String prenume, int ziua, int luna, int an) {
try {
pstmt = conn.prepareStatement("INSERT INTO persoane(Nume,Prenume,Ziua,Luna,An)VALUES(?,?,?,?,?);");
pstmt.setString(1, nume);
pstmt.setString(2, prenume);
pstmt.setInt(3, ziua);
pstmt.setInt(4, luna);
pstmt.setInt(5, an);
pstmt.execute();
} catch (SQLException e) {
System.out.println("Eroare prepareSql.");
}
}
public void selectsql() {
try {
rrs = stmt.executeQuery(query);
} catch (SQLException e) {
System.out.println("Error");
}
}
public void returnareRezultat() throws SQLException {
while (rrs.next()) {
String nume = rrs.getString("FirstName");
String prenume = rrs.getString("LastName");
int ziua = rrs.getInt("Day");
int luna = rrs.getInt("Month");
int an = rrs.getInt("Year");
// print the results
System.out.format("%s, %s, %s, %s, %s\n", nume, prenume, ziua, luna, an);
}
}
}
而這正是我調用該方法從我的GUI部分:
........................
INT zi_actuala = data_azi.get(Calendar.DAY_OF_MONTH); int luna_actuala = data_azi.get(Calendar.MONTH);
mysql.query = "SELECT Nume ,Prenume,Ziua,Luna,An FROM persoane WHERE (Ziua="+zi_actuala+" AND Luna="+luna_actuala+"); ";
mysql.selectsql();
.................
我的錯誤是在rrs = stmt.executeQuery(query);
(顯示java.lang.NullPointerException)
這是完全錯誤at MysqlConnect.selectsql(MysqlConnect.java:63) at GuiApp.<init>(GuiApp.java:120) at GuiApp$1.run(GuiApp.java:55) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
你能幫我嗎?提前致謝。
mysql.query = 「SELECT Nume,Prenume,Ziua,露娜,一種FROM persoane WHERE(Ziua =」 + zi_actuala + 「AND露娜= 」+ luna_actuala +「);」; –