Connection conn = null;
//String sql = "insert into test.test values ('"+idF.getText()+"','"+nameF.getText()+"','"+sexF.getText()+"','"+phoneF.getText()+"','"+addrF.getText()+"')";
String sql = "insert into test.test values ('1111','ght','m','1234','henan')";
try{
System.out.println("starting init connection");
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","admin","");
}catch(Exception ex){ex.printStackTrace();}
System.out.println("init connection successfully");
Statement sta = conn.createStatement();
int i = sta.executeUpdate(sql);
sta.close();
conn.commit();
System.out.println("whether successfully insert?");
/*if(rs.next()){
System.out.println("excute successfully");
}
else{
System.out.println("excute failure");
}*/
System.out.println("start to close connection");
try{
if(conn!=null){
conn.close();
conn = null;
}
}catch(Exception ex){ex.printStackTrace();}程序如上,执行时在控制台输出starting init connection 
init connection successfully 后就没了反应,也不报错,当然数据库也没更新。摆弄了以整天了,着急中……,请哪位弟兄指点,谢谢了

解决方案 »

  1.   


    Connection conn = null; 
    //String sql = "insert into test.test values ('"+idF.getText()+"','"+nameF.getText()+"','"+sexF.getText()+"','"+phoneF.getText()+"','"+addrF.getText()+"')"; 
    String sql = "insert into test.test values ('1111','ght','m','1234','henan')"; 
    try{ 
    System.out.println("starting init connection"); 
    try{ 
    Class.forName("com.mysql.jdbc.Driver"); 
    conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","admin",""); 
    }catch(Exception ex){ex.printStackTrace();} 
    System.out.println("init connection successfully"); 
    Statement sta = conn.createStatement(); 
    int i = sta.executeUpdate(sql); 
    sta.close(); 
    conn.commit(); 
    System.out.println("whether successfully insert?"); 
    /*if(rs.next()){ 
    System.out.println("excute successfully"); 

    else{ 
    System.out.println("excute failure"); 
    }*/ 
    System.out.println("start to close connection"); 
    try{ 
    if(conn!=null){ 
    conn.close(); 
    conn = null; 

    }catch(Exception ex){ex.printStackTrace();} 
      

  2.   


    package com.liujun.bbs;
    import java.sql.*;public class DB {
       public static Connection getConn(){
       Connection conn = null;
       try {
    Class.forName("org.gjt.mm.mysql.Driver");
       conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs","root","root");
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       
       return conn;
       }
       public static Statement creatStmt(Connection conn){
       Statement stmt = null;
       try {
     stmt = conn.createStatement();
    } catch (SQLException e) {
    e.printStackTrace();
    }
       return stmt;
       }
       public static ResultSet executeQuery(Statement stmt,String sql){
       ResultSet rs = null;
       try {
    rs = stmt.executeQuery(sql);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return rs;
       }
       public static void close(Connection conn){
       if(conn!= null){
       try {
    conn.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    conn = null;
       }
       }
       public static void close(Statement stmt){
       if(stmt!= null){
       try {
    stmt.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    stmt = null;
       }
       }
       public static void close(ResultSet rs){
       if(rs!= null){
       try {
    rs.close();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    rs = null;
       }
       }
       public static PreparedStatement prepareStmt(Connection conn,String sql){
       PreparedStatement pstmt = null;
       try {
    pstmt = conn.prepareStatement(sql);
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
       return pstmt;
       }
    }
      

  3.   

    To JOSY:sta.close();
    conn.commit();
    这两句是后来加上去的,原来没有的时候也不行
      

  4.   

    应该是这里出了问题:conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","admin",""); 
    你的数据库名称test后面应该有个问号的,这样:conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?","admin",""); 
    我在自己的数据库上试过了,没有问号就发生异常,加了过后就没事了,你试试吧!