日期:2014-05-16  浏览次数:20777 次

Myeclipse连接mysql代码
import java.sql.*; 
public class connect {
public static void main(String[] args){
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/scutcs";
String user = "root";
String password = "123456";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
System.out.println("Succeeded connecting to the Database!");
Statement statement = conn.createStatement();
String sql = "select * from student";
ResultSet rs = statement.executeQuery(sql);
String name = null;
while(rs.next())
{
name = rs.getString("name");
name = new String(name.getBytes("ISO-8859-1"),"GB2312");
System.out.println(rs.getString("id") + "\t" + name);  }
rs.close();
conn.close(); 
} catch(ClassNotFoundException e)
{   System.out.println("Sorry,can`t find the Driver!");
  e.printStackTrace(); 
}catch(SQLException e)
{   e.printStackTrace(); 
} catch(Exception e)
{   e.printStackTrace();   }
}