Java Oracle插入记录
我想从文本字段在数据库中插入一条记录,但它不起作用。我正在使用Oracle 10g。我对所有这些都是新手。我的全部代码都在这里,但不起作用的部分是私有的void jButton2ActionPerformed(java.awt.event.ActionEvent evt)按钮代码:Java Oracle插入记录,java,oracle,jdbc,sql-insert,Java,Oracle,Jdbc,Sql Insert,我想从文本字段在数据库中插入一条记录,但它不起作用。我正在使用Oracle 10g。我对所有这些都是新手。我的全部代码都在这里,但不起作用的部分是私有的void jButton2ActionPerformed(java.awt.event.ActionEvent evt)按钮代码: package project1; import java.sql.*; import java.sql.Connection; import java.sql.DriverManag
package project1;
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import static project1.InsertFrame.connection;
import javax.swing.table.DefaultTableModel;
public class CRUD extends javax.swing.JFrame {
//static Connection connection = null;
//Connection connection = null;
public CRUD() {
try {
initComponents();
String driverName = "oracle.jdbc.driver.OracleDriver";
Class.forName(driverName);
String serverName = "192.168.0.2";
String portNumber = "1521";
String sid = "XE";
String url = "jdbc:oracle:thin:@"+serverName+":"+portNumber+":"+sid;
String userName = "HR";
String password = "hr";
try {
connection = DriverManager.getConnection(url,userName,password);
} catch (SQLException ex) {
Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, ex);
}
try {
String temp="";
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT DISTINCT URUN_TIPI FROM KART");
while(rs.next()) // dönebildiği süre boyunca
{
String s = rs.getString("URUN_TIPI") ; //kolon isimleri oluşturuldu
temp+=s+"_";
}
Object [] tem_obj;
tem_obj=temp.split("_");
listOgrenciler.setListData(tem_obj);
} catch (SQLException ex) {
Logger.getLogger(edit.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE, null, ex);
}
listOgrenciler.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent arg0) {
if (!arg0.getValueIsAdjusting()) {
try {
Statement stmtx = connection.createStatement();
Object[] sss=listOgrenciler.getSelectedValues();
String swhere="" ;
for (int i = 0; i < sss.length; i++) {
swhere+=sss[i].toString()+",";
}
swhere=swhere.substring(0,swhere.length()-1);
ResultSet rsx = stmtx.executeQuery("SELECT * FROM KART where URUN_TIPI in ('"+swhere+"')") ;
String temp="";
while(rsx.next()) // dönebildiği süre boyunca
{
String s = " * "+rsx.getString("BOLGE")+" - "+rsx.getString("SEHIR")+" - "+rsx.getString("LOKASYON")+" - "+rsx.getString("SERI_NO")+" - "+rsx.getString("URUN_TIPI")+" - "+rsx.getString("URUN_KODU")+" - "+rsx.getString("FAZ")+" - "+rsx.getString("SEVK_TARIH")+" - "+rsx.getString("ACIKLAMA") ; //kolon isimleri oluşturuldu
temp+=s+"_";
}
Object [] tem_obj;
tem_obj=temp.split("_");
String ara="";
for (int i = 0; i < tem_obj.length; i++) {
ara+=tem_obj[i].toString()+"\n";
}
texttoarea.setText(ara);
} catch (SQLException ex)
{
Logger.getLogger(edit.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
String sqlInsert="INSERT INTO KART VALUES (?,?,?,?,?,?,?,?,?,?)";
PreparedStatement psta=connection.preparedStatement(sqlInsert);
psta.setString(1, jTextField1.getText());
psta.setString(2, jTextField2.getText());
psta.setString(3, jTextField3.getText());
psta.setString(4, jTextField4.getText());
psta.setString(5, jTextField5.getText());
psta.setString(6, jTextField6.getText());
psta.setString(7, jTextField7.getText());
psta.setString(8, jTextField8.getText());
psta.setString(9, jTextField9.getText());
psta.setString(10, jTextField10.getText());
psta.execute();
psta.close();
}
catch(Exception e)
{
System.out.println(e.getCause());
}
}
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(CRUD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(CRUD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(CRUD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(CRUD.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CRUD().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JScrollBar jScrollBar2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane3;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField10;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField jTextField6;
private javax.swing.JTextField jTextField7;
private javax.swing.JTextField jTextField8;
private javax.swing.JTextField jTextField9;
private javax.swing.JList listOgrenciler;
private javax.swing.JTextArea texttoarea;
// End of variables declaration
}
packageproject1;
导入java.sql.*;
导入java.sql.Connection;
导入java.sql.DriverManager;
导入java.sql.PreparedStatement;
导入java.sql.ResultSet;
导入java.sql.SQLException;
导入java.sql.Statement;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.swing.event.ListSelectionEvent;
导入javax.swing.event.ListSelectionListener;
导入静态project1.InsertFrame.connection;
导入javax.swing.table.DefaultTableModel;
公共类CRUD扩展了javax.swing.JFrame{
//静态连接=空;
//连接=空;
公共积垢(){
试一试{
初始化组件();
String driverName=“oracle.jdbc.driver.OracleDriver”;
类forName(driverName);
字符串serverName=“192.168.0.2”;
字符串portNumber=“1521”;
字符串sid=“XE”;
String url=“jdbc:oracle:thin:@”+serverName+:“+portNumber+”:“+sid;
字符串userName=“HR”;
字符串密码=“hr”;
试一试{
connection=DriverManager.getConnection(url、用户名、密码);
}catch(SQLException-ex){
Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE,null,ex);
}
试一试{
字符串temp=“”;
语句stmt=connection.createStatement();
结果集rs=stmt.executeQuery(“从KART中选择不同的URUN_TIPI”);
while(rs.next())//dönebildiği süre boyunca
{
String s=rs.getString(“URUN_TIPI”);//kolon isimleri oluşturuldu
温度+=s+“u2;”;
}
对象[]tem_obj;
tem_obj=温度拆分(“”);
listOgrenciler.setListData(tem_obj);
}catch(SQLException-ex){
Logger.getLogger(edit.class.getName()).log(Level.SEVERE,null,ex);
}
}捕获(ClassNotFoundException ex){
Logger.getLogger(CRUD.class.getName()).log(Level.SEVERE,null,ex);
}
addListSelectionListener(新建ListSelectionListener()){
@凌驾
public void值已更改(ListSelectionEvent arg0){
如果(!arg0.getValueIsAdjusting()){
试一试{
语句stmtx=connection.createStatement();
Object[]sss=listOgrenciler.getSelectedValues();
字符串swhere=“”;
对于(int i=0;icannot find symbol symbol:
method preparedStatement(String)
location: variable connection of type Connection