How to realize the call ACCESS query and insert the data used to
Import the Java. SQL. *;
Import the Java. The awt. *;
Import the Java. The awt. Event. An ActionEvent;
Import the Java. The awt. Event. ActionListener;
The import javax.mail. Swing. *;
Import the Java. The awt. Color;
The import javax.mail. Swing. JButton;
The import javax.mail. Swing. JFrame;
Public class AcessL extends JFrame {
Private static final ints DEFAULT_WEIDTH=1200;
Private static final ints DEFAULT_HEIGHT=780;
Private JScrollPane scpDemo;
Private data tabDemo;
Private Connection conn1;
Private Connection conn.
Private String connStr sqlStr;
Private String useName, passWord;
Private PreparedStatement ps;
Private StringBuffer strBuf=new StringBuffer ();
Public AcessL ()
{
JFrame f=new JFrame ();
F.s etTitle (" data extractor ");
F.s etDefaultCloseOperation (JFrame. EXIT_ON_CLOSE);
F.s etSize (DEFAULT_WEIDTH DEFAULT_HEIGHT);
F.s etVisible (true);
F.s etResizable (false);
F.s etLayout (null);
Enclosing scpDemo=new JScrollPane ();
This. ScpDemo. SetBounds (40110110, 0500);
JButton button=new JButton (" submit ");//instantiate the button and add the title
F.a dd (button);//button is added to the window that
Button. The setFont (new Font (" black ", 1, 30));
Button. SetBounds (350, 30, 500, 40).
Try {
BtnShow ();
} the catch (InstantiationException | IllegalAccessException e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
}
//to add components to form
F.a dd (enclosing scpDemo);
}
Public void btnShow () throws InstantiationException, IllegalAccessException {
String SQL="select * from student";
StrBuf. Delete (0, strBuf. Length ());
SqlStr="insert into student (ID, name1 name2) values (?,?,?,?,? ,? ,?) ";
Try {
//get connection
Class.forname (" com. HXTT. SQL. Access. AccessDriver "). The newInstance ();
System. Out.println (" driving load success!" );
Connection conn1=DriverManager. GetConnection (" JDBC: Access:///D:/1111/Database1. MDB ", ""," ");
System. Out.println (" database connection success!" );
PreparedStatement PSTM=conn1. PrepareStatement (SQL);
The ResultSet rs=PSTM. ExecuteQuery (SQL);
Class.forname (" com. Microsoft. Essentially. JDBC. SQLServerDriver ");//database driven load
System. Out.println (" driving load success!" );
ConnStr="JDBC: essentially://10.80.10.120:1433; DatabaseName=fhadmin ";//definition to connect to the database URL
UseName="sa".
PassWord="nmcy123.";
Conn=DriverManager. GetConnection (connStr useName, passWord);//connect to the database
System. Out.println (" database connection success!" );
//calculate how many records
int count=0;
While (rs), next ()) {
count++;
}
While (rs), next ()) {
Try
{
Ps=conn. PrepareStatement (sqlStr);
Ps. SetString (1, rs. Get string (" ID "));
Ps. SetString (2, "4");
Ps. SetString (3, "5");
Ps. ExecuteUpdate ();
} the catch (Exception e)
{
e.printStackTrace();
}
}
Rs=PSTM. ExecuteQuery ();
//will query log data, converted to suitable for generating data of data form
Object [] [] the info=new Object [count] [3];
Count=0;
While (rs), next ()) {
Info [count] [0]=rs. Get string (" ID ");
Info [count] [1]=rs. Get string (" name ");
The info [count] [2]=rs. Get string (" name1 ");
count++;
}
//define header
String [] title={" paper ", "test element 1", "test element 2"};
//this series
Enclosing tabDemo=new series (info, title);
//show the header
//this. JTH=this. TabDemo. GetTableHeader ();
//to add data to the panel with the scroll bar
Enclosing scpDemo. GetViewport (). The add (tabDemo);
rs.close();
Conn1. Close ();
Conn. Close ();
{} to catch (a ClassNotFoundException cnfe)
JOptionPane. ShowMessageDialog (null, "data error", "error", JOptionPane. ERROR_MESSAGE);
{} the catch (SQLException sqle)
JOptionPane. ShowMessageDialog (null, "data error", "error", JOptionPane. ERROR_MESSAGE);
}
}
Public static void main (String [] args)
{
New AcessL ();
}
}