2013年12月8日星期日

Help on Java to connect mysql database problem , but can be compiled to run abnormal

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/ / class mytest2
public class test2 {
public static void main (String args []) throws ClassNotFoundException, SQLException {
Connection con = JDBCTool.getConnection ("jdbc: mysql :/ / localhost: 3306/test", "root", "159753");

Statement st = con.createStatement ();
ResultSet re = st.executeQuery ("select * from stu");
while (re.next ()) {
String title = re.getString ("name");
re.getInt ("id");
System.out.println (title);
}
re.close ();
st.close ();
con.close ();
}
}



/ / class JDBCTool
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class JDBCTool {
public static Connection getConnection (String url, String user, String password) throws ClassNotFoundException, SQLException {
Class.forName ("com.mysql.jdbc.Driver");
return DriverManager.getConnection (url, user, password);

}
}
I do not know how it happens , the solution ! !


------ Solution ------------------------------------ --------
mysql connection jar package is not ah

------ For reference only ---------------------------------- -----

but the connection is successful ah

successfully run this code , ah. . . .


public class mytest7 {/ / load the jdbc driver is successfully tested
public static void main (String [] args) {
try {
Class.forName ("com.mysql.jdbc.Driver");
System.out.println (" find the driver , the connection is successful ! " ) ;
} catch (ClassNotFoundException e) {
System.out.println (" Can not find driver class ! " ) ;
}
}
}
------ For reference only --------------------------------- ------

but the connection is successful ah   
  
successfully run this code , ah. . . .   
  
  
public class mytest7 {/ / load the jdbc driver is successfully tested   
public static void main (String [] args) {   
try {   
Class.forName ("com.mysql.jdbc.Driver");   
System.out.println (" find the driver , the connection is successful ! " ) ;   
} catch (ClassNotFoundException e) {   
System.out.println (" Can not find driver class ! " ) ;   
}   
}   
}  


how the code is not in your original Riga print statements to try ? This error message is not the right place jar .
you look at it carefully



------ For reference only ---------------------------------- -----

but the connection is successful ah     
    
successfully run this code , ah. . . .     
    
    
public class mytest7 {/ / load the jdbc driver is successfully tested     
public static void main (String [] args) {     
try {     
Class.forName ("com.mysql.jdbc.Driver");     
System.out.println (" find the driver , the connection is successful ! " ) ;     
} catch (ClassNotFoundException e) {     
System.out.println (" Can not find driver class ! " ) ;     
}     
}     
}          
  
  
how the code is not in your original Riga print statements to try ? This error message is not the right place jar .   
you look at it carefully   
  
  
 
ah solve , thank

没有评论:

发表评论