import java.sql.*;
import java.awt.*;
import java.awt.event.*;public class JDBC
{
public static void main(String args[])throws Exception
{
new MyFrame("我的JDBC连接");
}
}
class MyFrame extends Frame implements ActionListener
{
Button btadd;
Button btn2;
Label outputLbl1=new Label(" ");
Label outputLbl2=new Label(" ");
Label outputLbl3=new Label(" ");
Label lab2;
TextField input1;
Label lab3;
TextField input2;
Label lab4;
TextField input3; MyFrame(String s)
{
super(s);
setLayout(null);
setSize(300,400);
Button btadd=new Button("添加");
Label outputL=new Label(" 学生信息:");
Label outputLbl1=new Label(" ");
Label outputLbl2=new Label(" ");
Label outputLbl3=new Label(" ");
Label output=new Label("请输入入学生信息进行添加");
Button btn2=new Button("查询");
Label lab2=new Label("学号:");
TextField input1=new TextField(10);
Label lab3=new Label("姓名:");
TextField input2=new TextField(6);
Label lab4=new Label("性别");
TextField input3=new TextField(4);
Label query=new Label("请输入学号进行查询:");
Label query1=new Label("学号");
TextField query2=new TextField(10);
Label result=new Label("查询结果: ");
setLayout(new FlowLayout());
add(outputL);
add(outputLbl1);
add(outputLbl2);
add(outputLbl3);
add(output); add(lab2);
add(input1);
add(lab3);
add(input2);
add(lab4);
add(input3);
add(btadd);
add(query);
add(query1);
add(query2);
add(btn2);
add(result);
btadd.addActionListener(this);
btn2.addActionListener(this);
//setBounds(50,50,100,200);
setVisible(true);
validate();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:student1","","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next()){ outputLbl1.setText("\t 学号:"+rs.getString(1));
outputLbl2.setText("\t 姓名:"+rs.getString(2));
outputLbl3.setText("\t 性别:"+rs.getString(3)+"\n");
stmt.executeUpdate("insert into student(学号,姓名,性别) values(101,'HAO','SDF')"); //执行插入操作
stmt.close();
rs.close();
}
}catch(Exception e){ e.printStackTrace(); }
//setSize(500,200);
setVisible(true);
pack();
show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btadd)
{
Connection con;
Statement stmt;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:student1";
con=DriverManager.getConnection(url);
stmt = con.createStatement();
String update = "insert into student values('input1.getText()','input2.getText()','input3.getText()')";
int result = stmt.executeUpdate(update);
if ( result > 0 )
{ System.out.println("update success!");
} else{
System.out.println("update fail!");
}
try{
if(!con.isClosed())
stmt.close();
con.close(); //关闭数据库连接
}catch(SQLException err){
System.err.println(err.toString());
}
}catch(Exception ae){ ae.printStackTrace(); }
}
if(e.getSource() == btn2)
{
try { Class.forName("ss.jdbc.odbc.JdbcOdbcDriver");
Connection con1=DriverManager.getConnection("jdbc:odbc:student1","","");
//加载JDBC-ODBC bridge驱动程序
PreparedStatement pstmt=con1.prepareStatement("select * from student where 学号 = 'query2.getText()'");
ResultSet rs1=pstmt.executeQuery();
rs1.close();
pstmt.close();
con1.close();
}catch(Exception e1) { }
}
}
}
import java.awt.*;
import java.awt.event.*;public class JDBC
{
public static void main(String args[])throws Exception
{
new MyFrame("我的JDBC连接");
}
}
class MyFrame extends Frame implements ActionListener
{
Button btadd;
Button btn2;
Label outputLbl1=new Label(" ");
Label outputLbl2=new Label(" ");
Label outputLbl3=new Label(" ");
Label lab2;
TextField input1;
Label lab3;
TextField input2;
Label lab4;
TextField input3; MyFrame(String s)
{
super(s);
setLayout(null);
setSize(300,400);
Button btadd=new Button("添加");
Label outputL=new Label(" 学生信息:");
Label outputLbl1=new Label(" ");
Label outputLbl2=new Label(" ");
Label outputLbl3=new Label(" ");
Label output=new Label("请输入入学生信息进行添加");
Button btn2=new Button("查询");
Label lab2=new Label("学号:");
TextField input1=new TextField(10);
Label lab3=new Label("姓名:");
TextField input2=new TextField(6);
Label lab4=new Label("性别");
TextField input3=new TextField(4);
Label query=new Label("请输入学号进行查询:");
Label query1=new Label("学号");
TextField query2=new TextField(10);
Label result=new Label("查询结果: ");
setLayout(new FlowLayout());
add(outputL);
add(outputLbl1);
add(outputLbl2);
add(outputLbl3);
add(output); add(lab2);
add(input1);
add(lab3);
add(input2);
add(lab4);
add(input3);
add(btadd);
add(query);
add(query1);
add(query2);
add(btn2);
add(result);
btadd.addActionListener(this);
btn2.addActionListener(this);
//setBounds(50,50,100,200);
setVisible(true);
validate();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:student1","","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next()){ outputLbl1.setText("\t 学号:"+rs.getString(1));
outputLbl2.setText("\t 姓名:"+rs.getString(2));
outputLbl3.setText("\t 性别:"+rs.getString(3)+"\n");
stmt.executeUpdate("insert into student(学号,姓名,性别) values(101,'HAO','SDF')"); //执行插入操作
stmt.close();
rs.close();
}
}catch(Exception e){ e.printStackTrace(); }
//setSize(500,200);
setVisible(true);
pack();
show();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btadd)
{
Connection con;
Statement stmt;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:student1";
con=DriverManager.getConnection(url);
stmt = con.createStatement();
String update = "insert into student values('input1.getText()','input2.getText()','input3.getText()')";
int result = stmt.executeUpdate(update);
if ( result > 0 )
{ System.out.println("update success!");
} else{
System.out.println("update fail!");
}
try{
if(!con.isClosed())
stmt.close();
con.close(); //关闭数据库连接
}catch(SQLException err){
System.err.println(err.toString());
}
}catch(Exception ae){ ae.printStackTrace(); }
}
if(e.getSource() == btn2)
{
try { Class.forName("ss.jdbc.odbc.JdbcOdbcDriver");
Connection con1=DriverManager.getConnection("jdbc:odbc:student1","","");
//加载JDBC-ODBC bridge驱动程序
PreparedStatement pstmt=con1.prepareStatement("select * from student where 学号 = 'query2.getText()'");
ResultSet rs1=pstmt.executeQuery();
rs1.close();
pstmt.close();
con1.close();
}catch(Exception e1) { }
}
}
}
是运行不来??还是代码报错??还是数据库连接出错???另外;
DriverManager.getConnection("jdbc:odbc:student1","","");
连接的时候用户名和密码都不用的???