开发者社区> 问答> 正文

java连接SQL server2008报错

蛮大人123 2016-06-06 17:31:49 1416

代码:

import java.sql.*;
public class SQL {
static Connection con;
static Statement sql;
static ResultSet res;
public Connection getConnection(){
    try{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        System.out.println("数据库驱动加载成功");
    }catch(Exception e){
        e.printStackTrace();
    }
    try{
        con=DriverManager.getConnection("jdbc:odbc:DemoDB", "sa", "3344521as");
        System.out.println("数据库连接成功");
    }catch(Exception e){
        e.printStackTrace();
    }
    return con;
}
public static void main(String[] args){
    SQL c=new SQL();
    c.getConnection();
    try{
        sql=con.createStatement();
        res=sql.executeQuery("select * from CommodityInfo");
        while(res.next()){
            float price=res.getFloat("InPrice");
            String name=res.getString("CommodityName");
            int amount=res.getInt("Amount");
            System.out.println("价格:"+price);
            System.out.println("商品名称:"+name);
            System.out.println("总数量:"+amount);
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}
}

代码报错如下:

at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121)
at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java:3117)
at sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:337)
at sun.jdbc.odbc.JdbcOdbcStatement.executeQuery(JdbcOdbcStatement.java:252)
at SQL.main(SQL.java:28)

我在用JDBC-ODBC桥梁连接数据库是发生以上报错,初学java不知道怎么解决?

SQL Java 数据库连接 数据库
分享到
取消 提交回答
全部回答(1)
  • 蛮大人123
    2019-07-17 19:29:06
    public class ConnectionSqlServer {
    
        private static Logger logger = Logger.getLogger(ConnectionSqlServer.class.getName());
    
        static String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
        static String dbURL = "jdbc:sqlserver://IP:10088;databasename=NAME"; // 1433是端口,"USCSecondhandMarketDB"是数据库名称
        static String userName = "USER"; // 用户名
        static String userPwd = "PWD"; // 密码
    
    
    
        public   Connection getConn() {
    
            Connection dbConn = null;
    
            try {
                dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
    
            } catch (Exception e) {
                e.printStackTrace();
    
                logger.error("ConnectionSqlServer:Exception"+e.getMessage());
            } 
            return dbConn;
        }
    
        @Test
        public   void sqlINSERT() throws SQLException{
             ConnectionSqlServer getConnectionSqlServer = new  ConnectionSqlServer();
             Connection dbConn = getConnectionSqlServer.getConn();
             Statement sta=dbConn.createStatement();
             sta.execute("INSERT INTO IpAddressArea VALUES('198.168.2.10','中国11','01','北京','010','海淀区','100084','五道口','12346','北京','010','五道口','12346');");
    
        }
    
        @Test
        public void sqlQuery() throws SQLException{
          ConnectionSqlServer getConnectionSqlServer = new  ConnectionSqlServer();
          Connection dbConn = getConnectionSqlServer.getConn();
          Statement sta=dbConn.createStatement();
          ResultSet rs=sta.executeQuery("SELECT * FROM IpAddressArea WHERE Ip = '61.54.105.49'");
          while(rs.next()){
              String Ip=rs.getString("Ip");
              System.out.println(Ip);
          }
        }
    }
    0 0
+ 订阅

分享数据库前沿,解构实战干货,推动数据库技术变革

推荐文章
相似问题