package Dao;import java.sql.*;public class sql { public static void main(String[] args) { Connection con; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/bus"; String user = "root"; String password = "123456"; try { Class.forName(driver); System.out.println("1"); con = DriverManager.getConnection(url,user,password); if(!con.isClosed()) System.out.println("Succeeded connecting to the Database!"); Statement statement = con.createStatement(); String sql = "select * from user"; ResultSet rs = statement.executeQuery(sql); System.out.println("-----------------"); System.out.println("执行结果如下所示:"); System.out.println("-----------------"); System.out.println(" 学号" + "\t" + " 姓名"); System.out.println("-----------------"); System.out.println("ok"); String name = null; String id = null; while(rs.next()){ name = rs.getString("name"); id = rs.getString("password"); name = new String(name.getBytes("ISO-8859-1"),"gb2312"); System.out.println(id + "\t" + name); } rs.close(); con.close(); } catch(ClassNotFoundException e) { System.out.println("Sorry,can`t find the Driver!"); e.printStackTrace(); } catch(SQLException e) { System.out.println("2"); e.printStackTrace(); }catch (Exception e) { e.printStackTrace(); }finally{ System.out.println("数据库数据成功获取!!"); } } }
添加回答
举报
0/150
提交
取消