/**这个例子要实现向已有的数据库中插入一条数据。*/importjava.sql.*;importorg.omg.CORBA.portable.Streamable;importcom.mysql.jdbc.Statement;publicclassTestDML{publicstaticvo...
/*
* 这个例子要实现向已有的数据库中插入一条数据。
*/
import java.sql.*;
import org.omg.CORBA.portable.Streamable;
import com.mysql.jdbc.Statement;
public class TestDML {
public static void main(String[]args){
Statement stmt = null;
Connection conn = null;
try{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//与数据库连接
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest","simaben","simaben");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//字符串的形式定义一条sql插入语句。
String sql = "insert into jdbctest values('ben','3','20')";
//执行语句。
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}finally{
try{
if(stmt != null){
stmt.close();
stmt = null;
}
if(conn != null){
conn.close();
conn = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
展开