日期:2014-05-20 浏览次数:21027 次
import java.sql.*;
public class ConnectMySql {
private static final String DBDRIVER="org.gjt.mysql.mm.Driver";
private static final String DBURL="jdbc:mysql://localhost:3306/pro";
private Connection conn;
private PreparedStatement psm;
public void insertData(){
try{
Class.forName(DBDRIVER);
}catch(ClassNotFoundException e){
System.err.println("driver fail to loading");
}
try{
conn=DriverManager.getConnection(DBURL,"root","root");
String sql="INSERT INTO expense(a,b,c,d) VALUES(?,?,?,?)";
psm=conn.prepareStatement(sql);
psm.setInt(1, 300);
psm.setString(2, "30");
psm.setString(3, "1111111");
psm.setString(4, "abcdefg");
}catch(Exception ee){
ee.printStackTrace();
}
try{
psm.close();
conn.close();
}catch(Exception eee){
eee.printStackTrace();
}
}
}