ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Myeclipse连接mysql代码:

Myeclipse连接mysql代码:

原创 Linux操作系统 作者:一眼泉水 时间:2010-11-16 08:37:00 0 删除 编辑

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();   }

 }  

} 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/24695024/viewspace-678190/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: mysql常用命令
下一篇: tomcat访问列表
请登录后发表评论 登录
全部评论

注册时间:2010-10-02

  • 博文量
    43
  • 访问量
    131323