Web工程下的test.java文件
需要导入三个jar文件
test.java
- import java.sql.*;
- public class test {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String driver ="com.mysql.jdbc.Driver";
- String url ="jdbc:mysql://127.0.0.1:3306/world";
- String user ="root";
- String password ="root";
- 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 city";
- 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();
- }
- }
- }