[求助]数据库疑惑
我在编写JDBC时对数据库进行插入语句时有一段代码,它不执行结果.代码如下:
import java.sql.*;
public class Demo{
protected final String driver="sun.jdbc.odbc.JdbcOdbcDriver";
protected final String source="jdbc:odbc:db1";
protected Connection connection;
protected Statement statement;
public void mm()throws SQLException{
try{
Class.forName(driver);
connection=DriverManager.getConnection(source);
statement=connection.createStatement();
statement.executeUpdate(s);
statement.close();
}catch(SQLException e){
e.printStackTrace();
}catch(Exception exc){
exc.printStackTrace();
}
}
public static void main(String[] args) throws SQLException{
Demo m=new Demo();
m.mm();
}
}
但是我加入一些代码后就可以了,为什么?
import java.sql.*;
public class Demo{
protected final String driver="sun.jdbc.odbc.JdbcOdbcDriver";
protected final String source="jdbc:odbc:db1";
protected Connection connection;
protected Statement statement;
public void mm()throws SQLException{
try{
Class.forName(driver);
connection=DriverManager.getConnection(source);
statement=connection.createStatement();
String sql="SELECT * FROM db1";
String s="INSERT INTO db1(id,name) VALUES (8,'ttom')";
statement.executeUpdate(s);
ResultSet rs=statement.executeQuery(sql);
while(rs.next()){
String id=rs.getString("id");
String name=rs.getString("name");
System.out.println("ID:"+"\t"+"NAME:");
System.out.println(id+"\t"+name);
}
statement.close();
}catch(SQLException e){
e.printStackTrace();
}catch(Exception exc){
exc.printStackTrace();
}
}
public static void main(String[] args) throws SQLException{
Demo m=new Demo();
m.mm();
}
}