0
RS始終是真實的,即使表的員工是不存在的servlet的數據庫,要創建表只有當表不存在於數據庫 的,哪些是SELECT count(*)FROM INFORMATION_SCHEMA.TABLES WHERE(TABLE_SCHEMA = 'servlet') AND (TABLE_NAME = 'employee')"
當PreparedStement執行和結果存儲ResultSet中時表不存在,當表是有兩種情況RS始終是真實的,即使表的員工是不存在的servlet數據庫
public class Table {
public static void main(String[] args)
{
Connection con=null;
PreparedStatement pstmt=null;
PreparedStatement pstmt1=null;
PreparedStatement pstmt2=null;
ResultSet rs=null;
String qry="SELECT count(*)FROM INFORMATION_SCHEMA.TABLES WHERE(TABLE_SCHEMA = 'servlet') AND (TABLE_NAME = 'employee')";
String qry1="CREATE TABLE servlet.Employee (" +
" ID INT(6) NOT NULL AUTO_INCREMENT, " +
" NAME VARCHAR(50) NOT NULL, " +
" Department varchar(20) NOT NULL, " +
" Salary DECIMAL(8,2) NOT NULL, " +
" PRIMARY KEY (ID))";
String qry2="insert into servlet.Employee values(?,?,?,?)";
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306?user=root&password=passworD");
pstmt=con.prepareStatement(qry);
rs=pstmt.executeQuery();
System.out.println(rs.next());
if(true==!rs.next()){
pstmt1=con.prepareStatement(qry1);
pstmt1.executeUpdate();
pstmt2=con.prepareStatement(qry2);
pstmt2.setInt(1,id);
pstmt2.setString(2, name);
pstmt2.setString(3, dept);
pstmt2.setDouble(4, salary);
pstmt2.executeUpdate();
}
else{
pstmt2=con.prepareStatement(qry2);
pstmt2.setInt(1,id);
pstmt2.setString(2, name);
pstmt2.setString(3, dept);
pstmt2.setDouble(4, salary);
pstmt2.executeUpdate();
}
}
}
謝謝,但爲什麼rs總是對的 –
你的問題解決了嗎? – randominstanceOfLivingThing
是的我的問題已經解決,但我不明白什麼是錯誤的邏輯應用我的(!rs.next())是真正的時,表不存在,它應該只有當表不存在時,其餘的代碼應該像你糾正的一樣工作@randominstanceoflivingthing –