zoukankan      html  css  js  c++  java
  • JAVA使用JDBC连接MySQL数据库 一

    public class JDBCTest {
    
        public static void main(String[] args){
            String driver = "com.mysql.jdbc.Driver";
            String url= "jdbc:mysql://localhost:3306/test";
            String user = "root";
            String password = "123456";
            try{
                 // 加载驱动程序
                Class.forName(driver);
                // 连续数据库
                Connection conn = (Connection) DriverManager.getConnection(url, user, password);
                if(!conn.isClosed()) 
                  System.out.println("Succeeded connecting to the Database!");
                // statement用来执行SQL语句
                Statement statement = (Statement) conn.createStatement();
                // 要执行的SQL语句
                String sql = "select * from employee";
                // 结果集
                ResultSet rs = (ResultSet) statement.executeQuery(sql);
                System.out.println("-----------------");
                System.out.println("姓名" +"	"+ "邮箱" +"	"+ "日期");
                System.out.println("-----------------");
                
                while(rs.next()) {
                    // 从mysql中获取数据
                    String uname = rs.getString("name");
                    String uemail = rs.getString("email");
                    String uhiredate = rs.getString("hiredate");
                    // 首先使用ISO-8859-1字符集将name解码为字节序列并将结果存储新的字节数组中。
                    // 然后使用GB2312字符集解码指定的字节数组
                    //name = new String(name.getBytes("gb2312"),"gb2312");
                    // 输出结果
                    System.out.println(uname +"	"+ uemail +"	"+ uhiredate);
                }
                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();
                } 
        }
    }

    结果如下:

    使用dos查看数据库结果如下:

  • 相关阅读:
    uva 816
    SQL语言类
    VTK中国文字显示和简单的医疗图像浏览软件
    系统开始了解--感兴趣的文章
    的天数,以一个日期与当前日期的
    POJ 2528 QAQ段树+分离
    IT该忍者神龟Instant client required
    进程和线程之间的通信
    2014——我的求职(两)
    如何绕过chrome的弹窗拦截机制
  • 原文地址:https://www.cnblogs.com/liushao/p/6379373.html
Copyright © 2011-2022 走看看