import java.sql.*;
public class ConnToMysql{
public static void main(String args[]){
Connection conn;
Statement stmt;
ResultSet rs;
String sql;
try{
Class.forName("org.gjt.mm.mysql.Driver".newInstance();
}catch (ClassNotFoundException e) {
System.out.println(e.toString()); //To change body of catch statement use File | Settings | File Templates.
} catch(java.lang.IllegalAccessException e){
System.out.println(e.toString());
}catch(java.lang.InstantiationException e){
System.out.println(e.toString());
}
try {
conn=DriverManager.getConnection("jdbc:mysql://localhost/mydatabase?user=root&password=&useUnicode=true&characterEncoding=8859_1";
stmt=conn.createStatement();
sql="select * from student";
rs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
System.out.println(e.toString()); //To change body of catch statement use File | Settings | File Templates.
}
}
}
错误信息如下:
java.sql.SQLException: Cannot connect to MySQL server on localhost:3306. Is ther
e a MySQL server running on the machine/port you are trying to connect to? (java
.lang.NumberFormatException)
--转自