stu表
三个字段:no,name,编号
查询没有问题,就是更新有问题update那里
import java.awt.*;
import java.awt.event.*;import javax.swing.*;
import java.sql.*;
public class zhsql extends JFrame
{ Connection con=null;
Statement st=null;
ResultSet rs=null;
// JButtons to represent security keypad
private JButton oneJButton;
// JLabel, JTextArea and JScrollPane to display access log
private JTextArea accessLogJTextArea;
private Connection myConnection ;
private Statement myStatement;
private ResultSet myResultSet;public void connect()
{try
{ String dbUr1="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=stu.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
myConnection=DriverManager.getConnection(dbUr1, "","" );
myStatement=myConnection.createStatement();
}
catch (Exception cnfex )
{
}
}
private void select()
{
try {
myResultSet= myStatement.executeQuery("SELECT * from stu");
while (myResultSet.next())
{
accessLogJTextArea.append( myResultSet.getString("name")+"\n");
}
myResultSet.close();
}
catch ( SQLException sqlex ) {}
}
private void update()
{int ii=77;
try {
myStatement.executeQuery("UPDATE stu SET no='"66"',name='"aaaa"',编号='"2"'"); myResultSet.close();
}
catch ( SQLException sqlex ) {}
} // no-argument constructor
public zhsql()
{
createUserInterface();
} // create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up oneJButton
oneJButton = new JButton();
oneJButton.setBounds( 80, 64, 50, 50 );
oneJButton.setText( "1" );
contentPane.add( oneJButton );
oneJButton.addActionListener( new ActionListener() // anonymous inner class
{
// event handler called when oneJButton is pressed
public void actionPerformed( ActionEvent event )
{
oneJButtonActionPerformed( event );
} } // end anonymous inner class ); // end call to addActionListener
accessLogJTextArea = new JTextArea();
accessLogJTextArea.setBounds( 280, 214, 250, 250 );
accessLogJTextArea.setText( "" );
contentPane.add( accessLogJTextArea ); // set up accessLogJScrollPane
// set properties of application's window
setTitle( "Security Panel" ); // set title bar string
setSize( 810, 750 ); // set window's size
setVisible( true ); // display window
}
// end method createUserInterface // append 1 to the security code
private void oneJButtonActionPerformed( ActionEvent event )
{ connect();
select();
update();
} // end method oneJButtonActionPerformed // append 2 to the security code
// main method
public static void main( String[] args )
{
zhsql application = new zhsql();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main}
三个字段:no,name,编号
查询没有问题,就是更新有问题update那里
import java.awt.*;
import java.awt.event.*;import javax.swing.*;
import java.sql.*;
public class zhsql extends JFrame
{ Connection con=null;
Statement st=null;
ResultSet rs=null;
// JButtons to represent security keypad
private JButton oneJButton;
// JLabel, JTextArea and JScrollPane to display access log
private JTextArea accessLogJTextArea;
private Connection myConnection ;
private Statement myStatement;
private ResultSet myResultSet;public void connect()
{try
{ String dbUr1="jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=stu.mdb";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") ;
myConnection=DriverManager.getConnection(dbUr1, "","" );
myStatement=myConnection.createStatement();
}
catch (Exception cnfex )
{
}
}
private void select()
{
try {
myResultSet= myStatement.executeQuery("SELECT * from stu");
while (myResultSet.next())
{
accessLogJTextArea.append( myResultSet.getString("name")+"\n");
}
myResultSet.close();
}
catch ( SQLException sqlex ) {}
}
private void update()
{int ii=77;
try {
myStatement.executeQuery("UPDATE stu SET no='"66"',name='"aaaa"',编号='"2"'"); myResultSet.close();
}
catch ( SQLException sqlex ) {}
} // no-argument constructor
public zhsql()
{
createUserInterface();
} // create and position GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up oneJButton
oneJButton = new JButton();
oneJButton.setBounds( 80, 64, 50, 50 );
oneJButton.setText( "1" );
contentPane.add( oneJButton );
oneJButton.addActionListener( new ActionListener() // anonymous inner class
{
// event handler called when oneJButton is pressed
public void actionPerformed( ActionEvent event )
{
oneJButtonActionPerformed( event );
} } // end anonymous inner class ); // end call to addActionListener
accessLogJTextArea = new JTextArea();
accessLogJTextArea.setBounds( 280, 214, 250, 250 );
accessLogJTextArea.setText( "" );
contentPane.add( accessLogJTextArea ); // set up accessLogJScrollPane
// set properties of application's window
setTitle( "Security Panel" ); // set title bar string
setSize( 810, 750 ); // set window's size
setVisible( true ); // display window
}
// end method createUserInterface // append 1 to the security code
private void oneJButtonActionPerformed( ActionEvent event )
{ connect();
select();
update();
} // end method oneJButtonActionPerformed // append 2 to the security code
// main method
public static void main( String[] args )
{
zhsql application = new zhsql();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } // end method main}
║ 免费的源码、工具网站,欢迎大家访问!║
║ http://www.j2soft.cn/ ║
╰═══════════════════╯
在插入时不能使用executeQuery,应该使用executeUpdate()
executeUpdate()这两个函数的参数是字符串,你这编译也过不去啊,别说执行,连+号也不给啊
executeUpdate("UPDATE stu SET no='"+var1+"',name='"+var2+"',编号='"+var3+"'")