日期:2014-05-17 浏览次数:20666 次
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class TestDML2 {
public static void main(String[] args) {
System.out.println("请输入要插入的数据,格式为deptno dname loc");
Scanner scanner=new Scanner(System.in);
int deptno=scanner.nextInt();
String dname=scanner.next();
String loc=scanner.next();
System.out.println("insert into dept values("+deptno+",'"+dname+"','"+loc+"')");
insert(deptno,dname,loc);
}
private static void insert(int deptno, String dname, String loc) {
Connection conn=null;
Statement stmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydata", "root", "yj1039");
stmt.executeUpdate("insert into dept values("+deptno+",'"+dname+"','"+loc+"')");
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally{
try{
if(stmt!=null){
stmt.close();
stmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}