zoukankan      html  css  js  c++  java
  • java连接数据库的两种方法总结

    方法一:使用jdbc-odbc桥连接sql server,作为中间媒介连接数据库

    • 1.配置数据源:打开控制面版->管理工具->数据源(ODBC)->选用户DSN,按下添加按钮->sql server->写下数据源的名字(假设是test),再写下连接的服务器名(一般默认为local或.)->更改默认的数据库为(勾选你想要连接的数据库)->接下来一直确定就完成了数据源的配置.
    • 2.加载驱动:Class.forNmae("sun.jdbc.odbc.JdbcOdbcDriver");
    • 3.得到连接:Connection ct=DriverManager.getConnection("jdbc:odbc:test");test是配置数据源的时候的数据源名字
    • 4.创建preparedStateMent,这个语句主要是用于发送sql语句到数据库.
    • 5.最后关闭资源

    代码:

    import java.sql.*;
    public class preparedStatement {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
        public static void main(String[] args) {
            Connection ct = null;
            PreparedStatement ps = null;
            ResultSet rs=null;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
            try {
                //加载驱动
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                //得到连接
                ct=DriverManager.getConnection("jdbc:odbc:test");
                //创建一个PreparedStatement,用于向数据库放松sql语句
                ps=ct.prepareStatement("select * from 产品名称表");
                //得到结果
                rs=ps.executeQuery();//这个方法适用于从表中查找数据
                //如果要向表中插入,删除,更新数据需要使用方法executeUpdate();
                while(rs.next()){//这里rs.next()一定要使用next()方法,否则有空指针错误
                    String number=rs.getString(1);
                    String string=rs.getString(2);
                    System.out.println(number+" "+string);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 关闭资源,记住关闭资源的顺序,根据得到赋值的反顺序进行关闭资源
                try {
                    if(rs!=null){
                        rs.close();
                    }
                    if (ps != null)
                        ps.close();
                    if (ct != null)
                        ct.close();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
    

    方法二:使用驱动程序直接操作数据库(这个方法更多人使用,但是前期所做的工作更多)

    • 1.需要引入jar包,sql server2008只要引入sqljdbc4.jar包就行了。这个包百度上有下载。
      这是我下载的网址:http://download.csdn.net/download/a785143175/5163519
      导入方法:工具栏project选项->properties->java Build path中选择libraries->点击add Externar jars按钮->然后在你电脑中选中sqljdbc4.jar导入进去
    • 2.连接数据库一般是使用sql验证,所以需要你自己建立帐号密码。
      方法:先用windows验证进入数据库,然后在安全性中点击鼠标右键新建登录名,勾选SQL身份验证,将强制密码过期不选,点击确定。然后断开连接,重新以现在的用户名和密码连接。然后在这个用户名中新建数据库就好。
    • 3、接下来的代码部分跟odbc类似,有些小小的不同。

    代码:

    import java.sql.*;
    public class jdbc1 {
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Connection ct = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                //第一步,加载驱动
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                //得到连接
                ct=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=liuyan","sa","3209554");
                //创建PreparedStatement
                ps=ct.prepareStatement("select * from userinformation where name='张三丰'");                                            
                rs=ps.executeQuery();
                //这个方法适用于从表中查找数据
                //如果要向表中插入,删除,更新数据需要使用方法executeUpdate();
                while(rs.next()){//这里rs.next()一定要使用next()方法,否则有空指针错误
                    String number=rs.getString(1);
                    String string=rs.getString(2);
                    String string2=rs.getString(3);
                    System.out.println(number+" "+string+" "+string2);                                                
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally{
                try {
                    if(rs!=null){
                        rs.close();
                    }
                    if (ps != null)
                        ps.close();
                    if (ct != null)
                        ct.close();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
    

    几点注意

    • 我们每次在连接数据库前都必须要引入sql包:import java.sql.*

    • 我们还可以使用?赋值的方法
      ps=ct.prepareStatement("select * from 产品名称表 whrer 产品名称=?");
      ps.setString(1,"手机");//表示在第一个问号上添加“手机”的字符串.如果是整数用setInt()方法;

    总结

    经过了一个下午和一个晚上的奋斗,终于将myeclipse和数据库连接成功了。中间出现了好多问题:找不到包(到现在也不知道为什么会找不到),空指针异常,通过端口1433连接到主机失败,远程过程调试失败。感觉都快要崩溃了,好在最后还是连上了。学习新东西的过程就是漫长又磨人的,但学成之后还是充满了成就感。

  • 相关阅读:
    动态传参
    函数的介绍
    文件的操作
    send email with formatted table
    minimize and close window with customed winform
    python algorithm
    something important about docker
    book list
    which language is suitable for what to do
    Find Duplicate Items in list fast
  • 原文地址:https://www.cnblogs.com/leijing/p/6914814.html
Copyright © 2011-2022 走看看