package dataBase;
import java.sql.*;
public class DBoperation{
Statement sta;//数据库操作状态
public static void main(String args[])
{try{JDBCDemo bridge=new JDBCDemo();
DBoperation operator=new DBoperation();
ResultSet rs;
String sqlCommand;
bridge.setURL("jdbc:odbc:Jstu");
bridge.setUser("");
bridge.setPassword("");
Connection conn=bridge.getConnection();
operator.setStatement(conn);
//查询
sqlCommand="select *from stuage";
System.out.println("COMMAND:"+sqlCommand);
rs=operator.executeQuery(sqlCommand);
rs.next();
for(int i=0;i
rs.next();}
//修改
sqlCommand="Update stuage set age=19 where name=\'TOM\'";
System.out.println("COMMAND:"+sqlCommand);
operator.executeUpdate(sqlCommand);
sqlCommand="select *from stuage";
rs=operator.executeQuery(sqlCommand);
rs.next();
for(int i=0;i
rs.next();}
//Insert
sqlCommand="Insert into stuage(ID,name,age) Values(\'1003\',\'Jerry\',\'20\')";
System.out.println("COMMAND:"+sqlCommand);
operator.executeInsert(sqlCommand);
sqlCommand="select *from stuage";
rs=operator.executeQuery(sqlCommand);
rs.next();
for(int i=0;i
rs.next();}
//..删除
sqlCommand="Delete from stuage where ID=\'1003\'";
System.out.println("COMMAND:"+sqlCommand);
operator.executeDelete(sqlCommand);
sqlCommand="select *from stuage";
rs=operator.executeQuery(sqlCommand);
rs.next();
for(int i=0;i
rs.next();}
//..
rs.close();
operator.closeStatement();
conn.close();}
catch(Exception e){}}
public void setStatement(Connection conn)
{try {this.sta=conn.createStatement(); }
catch(Exception e){}}
public ResultSet executeQuery(String sqlCommand)
{try {return sta.executeQuery(sqlCommand);}
catch(Exception e){}return null;}
public void closeStatement()
{try{sta.close();}catch(Exception e){}}
public void executeUpdate(String sqlCommand)
{try{sta.executeUpdate(sqlCommand);}catch(Exception e){}}
public void executeInsert(String sqlCommand)
{try{sta.executeUpdate(sqlCommand);}catch(Exception e){}}
public void executeDelete(String sqlCommand)
{try{sta.executeUpdate(sqlCommand);}catch(Exception e){}}
}
我在以前自己写的学生管理系统里面截一段好了
try{
String strurl="jdbc:odbc:StudentInfo";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con1 = DriverManager.getConnection(strurl);
Statement smt1 = con1.createStatement();
//从文本框中获得信息
number = text2.getText();
name = text1.getText();
sex = text3.getText();
IDnumber =text4.getText();
nation = text5.getText();
nativeplace = text6.getText();
polity = text7.getText();
academy = text8.getText();
speciality = text9.getText();
telephone = text10.getText();
address = text11.getText();
Remark = remark.getText();
//在数据库中新增一条学生信息
smt1.execute("INSERT INTO StudentInfo VALUES('"+number+"','"+name+"','"+sex+"','"+IDnumber+"','"+nation+"','"+nativeplace+"','"+polity+"','"+academy+"','"+speciality+"','"+telephone+"','"+address+"','"+Remark+"')");
JOptionPane.showMessageDialog(null,"信息录入成功!","提示",JOptionPane.INFORMATION_MESSAGE);
smt1.close();
con1.close();
}
catch(ClassNotFoundException a){
a.printStackTrace();
}
catch(SQLException h){
h.printStackTrace();
}