zoukankan      html  css  js  c++  java
  • eclipse 连接MySQL、SQL server数据库

    连接SQL server

    public class Test2 {
    
        public static void main(String[] args) {
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                System.out.println("加载数据库驱动成功");
                String url="jdbc:sqlserver://127.0.0.1:1433;DatabaseName=ipt";//声明数据库test的url
                String user="user";//数据库账号
                String pass="password";//数据库密码
                //建立数据库连接,获得连接对象conn
                Connection conn=DriverManager.getConnection(url,user,pass);
                System.out.println("数据库连接成功");
                Statement stmt=conn.createStatement();//创建一个Statement对象
                String sql="select * from cdr_raws";//生成一条sql语句
                ResultSet rs=stmt.executeQuery(sql);//执行查询,把查询结果赋值给结果集对象
                String callingPartyNumber;//声明2个变量分别为用户名,密码
                while(rs.next()){//遍历结果集
                    callingPartyNumber=rs.getString("callingPartyNumber");//
                    System.out.println(callingPartyNumber);
                }
                System.out.println("获得查询结果集");
                conn.close();
                System.out.println("关闭数据库连接对象");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }//加载数据库驱动
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    连接MySQL

    public class Test2 {
    
        public static void main(String[] args) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                System.out.println("加载数据库驱动成功");
                String url="jdbc:mysql://localhost:3306/test";//声明数据库test的url
                String user="root";//数据库账号
                String pass="pass";//数据库密码
                //建立数据库连接,获得连接对象conn
                Connection conn=DriverManager.getConnection(url,user,pass);
                System.out.println("数据库连接成功");
                Statement stmt=conn.createStatement();//创建一个Statement对象
                String sql="select * from account";//生成一条sql语句
                ResultSet rs=stmt.executeQuery(sql);//执行查询,把查询结果赋值给结果集对象
                String callingPartyNumber;//声明2个变量分别为用户名,密码
                while(rs.next()){//遍历结果集
                    callingPartyNumber=rs.getString("name");//
                    System.out.println(callingPartyNumber);
                }
                System.out.println("获得查询结果集");
                conn.close();
                System.out.println("关闭数据库连接对象");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }//加载数据库驱动
            catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
  • 相关阅读:
    Oracle GoldenGate OGG管理员手册(较早资料)
    Oracle GoldenGate DDL 详细说明 使用手册(较早资料)
    Script:when transaction will finish rollback
    Script:诊断解析等待和高version count
    Oracle DB 12.2(12cR2)的一个新特性:硬解析失败的SQL语句(需要符合一定条件)打印到alert_sid.log中.
    Oracle 补丁那些事儿(PS、PSU、CPU、SPU、BP、DBBP…)
    Shell: extract more from listener.log (分析oracle监听日志)
    12 Things Developers Will Love About Oracle Database 12c Release 2
    MySQL字符存储:charset-unicode-sets
    GBK与UTF-8编码错误转换后,无法再正确恢复
  • 原文地址:https://www.cnblogs.com/RealWorld/p/8989226.html
Copyright © 2011-2022 走看看