今天写了一下用GUI连接数据库,想用Applet网页连接,但是却报错,换成Frame就好了 。
为安全着想Applet有许多的限制,不知道用applet访问远程数据库是否也有限制??
还是我代码写错了报错?
简单代码如下。
import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.sql.*;
public class TestJdbcApp extends Frame
{
JTable jt;
JPanel jpSouth;
JButton jb1;
JButton jb2;
Object[][] rowData;
String[] fields;
Connection conn;
Statement st;
ResultSet rs;
int count = 1;
public void init() {
System.out.println("init");
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://192.168.0.234:3306/jdbc", "gunzi", "273326");
//3.create syntax
st = conn.createStatement();
//4.execute syntax
rs = st.executeQuery("select * from myjdbc");
rowData = new Object[6][5];
for (int i=0; i<6; i++)
{
rs.next();
for (int j=0; j<5; j++)
{
rowData[i][j] = rs.getObject(count);
count ++;
}
count = 1;
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(SQLException e) {
e.printStackTrace();
}finally{
try
{
if (rs!=null)
rs.close();
if (st!=null)
st.close();
if (conn!=null)
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
}
public void start() {
System.out.println("start");
fields = new String[] {"ID", "Name", "Address", "Money", "School"};
this.setLayout(new BorderLayout());
jt = new JTable(rowData, fields);
jt.setPreferredScrollableViewportSize(new Dimension(300, 80));
this.add(jt, "Center");
jpSouth = new JPanel();
jb1 = new JButton("提交");
jb2 = new JButton("取消");
jpSouth.add(jb1);
jpSouth.add(jb2);
this.add(jpSouth, "South");
this.setBounds(200,100,400,300);
this.setVisible(true);
}
public static void main(String[] args) {
TestJdbcApp tjb = new TestJdbcApp();
tjb.init();
tjb.start();
}
}