首页 > 代码库 > 2014=9=24 连接数据库2

2014=9=24 连接数据库2

package cn.sql.sqlconn;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class Util {    Connection conn=null;            public Util(){            // 第一步            try {            Class.forName("com.mysql.jdbc.Driver");        } catch (ClassNotFoundException e) {            // TODO Auto-generated catch block                        System.out.println("加载驱动失败");        }            //第二步        String url = "jdbc:mysql://10.1.5.205:3306/admin ";        String user = "root ";        String password = "admin ";        try {             conn =  DriverManager.getConnection(url, user, password) ;        } catch (SQLException e) {            // TODO Auto-generated catch block            System.out.println("数据库连接失败");        }                        //第三步  操作数据库        try {            Statement sta = conn.createStatement();            String sql  = "select * from userinfor ; " ;                        ResultSet rs = sta.executeQuery(sql);                        while(rs.next()){                String name= rs.getString("name");                String pwd= rs.getString("pwd");                System.out.println("name: "+name + "\n pwd: " +pwd);                            }                        //rs.close();            //sta.close();            //conn.close();                        } catch (SQLException e) {            // TODO Auto-generated catch block            System.out.println("数据库连接失败");        }                    }    public static void main(String[] args) {        // TODO Auto-generated method stub new Util();    }}

 

2014=9=24 连接数据库2