现在的位置: 首页 > 综合 > 正文

Myeclipse连接Mysql数据库

2017年12月19日 ⁄ 综合 ⁄ 共 1515字 ⁄ 字号 评论关闭

Web工程下的test.java文件

需要导入三个jar文件

test.java

  1. import java.sql.*; 
  2. public class test { 
  3.     public static void main(String[] args) { 
  4.         // TODO Auto-generated method stub 
  5.         String driver ="com.mysql.jdbc.Driver"
  6.         String url ="jdbc:mysql://127.0.0.1:3306/world"
  7.         String user ="root"
  8.         String password ="root"
  9.         try
  10.             Class.forName(driver); 
  11.             Connection conn = DriverManager.getConnection(url, user, password); 
  12.             if(!conn.isClosed()) 
  13.                 System.out.println("Succeeded connecting to the Database!"); 
  14.             Statement statement= conn.createStatement(); 
  15.             String sql ="select * from city"
  16.             ResultSet rs =statement.executeQuery(sql); 
  17.             String name =null
  18.             while(rs.next()) 
  19.             { 
  20.                 name = rs.getString("name"); 
  21.                 name =new String(name.getBytes("ISO-8859-1"),"GB2312"); 
  22.                 System.out.println(rs.getString("id") +"\t"+ name); } 
  23.                 rs.close(); 
  24.                 conn.close();  
  25.             } 
  26.             catch(ClassNotFoundException e) 
  27.             {   
  28.                 System.out.println("Sorry,can`t find the Driver!"); 
  29.                 e.printStackTrace();  
  30.             } 
  31.             catch(SQLException e) 
  32.             {  
  33.                 e.printStackTrace();  
  34.             } 
  35.             catch(Exception e) 
  36.             {  
  37.                 e.printStackTrace(); 
  38.             } 
  39.     } 
  40.  

 

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/815862

抱歉!评论已关闭.