我的主鍵是empid
和date
。我想要做的是員工向sql server插入值,如果他再次嘗試在同一天向數據庫插入值,那麼主鍵應該是衝突的。所以我想,如果他/她試圖做這樣的我如何從sqlserver檢索錯誤消息給我的java程序員
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
String g = jComboBox1.getSelectedItem().toString();
if(g.equals("--Select Your Employee ID--"))
{
jLabel6.setForeground(Color.RED);
jLabel6.setText("Please Select Your ID ");
}
else{
jLabel6.setText("");
}
String a =jTextArea1.getText();
if(a.equals("")){
jLabel7.setForeground(Color.RED);
jLabel7.setText("Please Fill the Report ");
}
else{
jLabel7.setText("");
}
try{
String url="jdbc:sqlserver://localhost:1433;databaseName=gym2 ";
String username = "mali";
String password = "12345";
Connection con =DriverManager.getConnection(url,username,password);
Statement st = con.createStatement();
ResultSet rs;
Calendar cal = Calendar.getInstance();
SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
String sql = "INSERT INTO report(empid,dat,rep) VALUES('"+g+"','"+format1.format(cal.getTime())+"','"+a+"')" ;
rs= st.executeQuery(sql);
jComboBox1.addItem("--Select Your Employee ID--");
}
catch(Exception e){
}
我可以得到它的標籤?不工作 –
我不明白你的評論。 –
它不工作我想在jframe中得到一個jable的結果 –