我開發了一個awt程序,現在我試圖使用ODBC橋將它連接到MS Access數據庫。需要幫助將awt應用程序連接到MS Access數據庫
下面是代碼: -
import java.awt.*;
import java.awt.event.*;
import java.awt.GridLayout;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.*;
class MyClass
{
public static void main(String args[])
{
Project prj=new Project();
}
}
class Project
{
Statement s;
Connection con;
String s1,s2;
TextField t1,t2;
Button b1,b2;
Project()
{
Frame f=new Frame("Registration");
f.setLayout(new GridLayout(2,1));
f.setSize(400,400);
Panel p1=new Panel();
p1.setLayout(new FlowLayout());
t1=new TextField(15);
t2=new TextField(30);
p1.add(new Label("Roll :"));
p1.add(t1);
p1.add(new Label("Name :"));
p1.add(t2);
f.add(p1);
Panel p2=new Panel();
p2.setLayout(new FlowLayout());
s1=t1.getText();
s2=t2.getText();
try{
try
{Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(Exception e){}
con=DriverManager.getConnection("jdbc:odbc:testdb");
s=con.createStatement();
}catch(SQLException e){System.out.println("Error in connecting to database"); }
b1=new Button("Submit");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
s.executeUpdate("insert into college(Roll,Names) values("+Integer.parseInt(s1)+","+s2+")");
}catch(SQLException se){
System.out.println(se);}
}});
b2=new Button("Quit");
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){System.exit(0);
}});
p2.add(b1);
p2.add(b2);
f.add(p2);
f.pack();
f.setVisible(true);
}
}
當我用下面的語句在
s.executeUpdate("insert into college(Roll,Names) values (555,'Naveen')");
值插入database.But的的ActionListener當我使用的說法,現在從文本字段中取值,
s.executeUpdate("insert into college(Roll,Names) values ("+Integer.parseInt(s1)+","+s2+")");
在上面的代碼中,我得到這個例外,我使用了try catch塊。
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
sql語句肯定有什麼問題。我在哪裏錯了?
您的S1沒有在此行有效的int的Integer.parseInt(S1),從而NFE .. – PermGenError
@ chaitanya10:S1包含作爲通過從文本字段中獲得的整數值字符串,所以我用Integer.parseInt(s1)將該字符串值轉換回int.What是問題? –
嘗試打印一次字符串 – PermGenError