赞
踩
- <pre name="code" class="java">import java.sql.*;
-
- public class JDBCTest {
-
- public static void main(String[] args){
-
- // 驱动程序名
- String driver = "com.mysql.jdbc.Driver";
-
- // URL指向要访问的数据库名scutcs
- String url = "jdbc:mysql://127.0.0.1:3306/student";
-
- // MySQL配置时的用户名
- String user = "root";
-
- // MySQL配置时的密码
- String password = "1681691689";
-
- try {
- // 加载驱动程序
- Class.forName(driver);
-
- // 连续数据库
- Connection conn = DriverManager.getConnection(url, user, password);
-
- if(!conn.isClosed())
- System.out.println("Succeeded connecting to the Database!");
-
- // statement用来执行SQL语句
- Statement stmt = conn.createStatement();
-
- // 要执行的SQL语句
- ResultSet rs =stmt.executeQuery( "select * from class1");
-
- while(rs.next()) {
-
- System.out.println(rs.getString("id") + "\t" + rs.getString("name") + "\t" + rs.getString("sex"));
- }
-
- rs.close();
- stmt.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();
-
-
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。