首页新闻找找看学习计划

Java与mysql数据库连接的问题,但始终连接不上

0
悬赏园豆:20 [已解决问题] 解决于 2015-06-27 14:52

package cn.com;

import java.util.*;

public class Company {  private static Scanner scr;

 public static void main(String[] args) {   

   Execute people;  

 people=new Execute();  

 people.setSourceName("Company");   

people.setTableName("Company_Employees");  

 System.out.println("【1】查询数据表的记录数");  

 System.out.println("【2】数据库的查询操作");   

System.out.println("【3】数据库的插入操作");   

System.out.println("【4】数据库的删除操作");  

 System.out.println("【5】数据库的更新操作");  

 System.out.println("请选择你要进行的数据库操作序号:");  

 scr = new Scanner(System.in);   

int num=scr.nextInt();   

while(num!=0)   {   

 switch(num){   

 case 1:{ people.NumberResult();break;}  

  case 2:{      System.out.println("请输入要进行查询的公司员工的编号:");      String com_id=scr.toString();      people.SelectResult(com_id);      break;    }   

case 3:{     String[] s=new String[7];     System.out.println("依次输入一条记录的信息:");     for(int i=0;i<7;i++){      s[i]=scr.toString();     }    people.InsertResult(s);    break;    }   

case 4:{     System.out.println("请输入要进行删除记录的公司员工的编号:");     String com_id=scr.toString();     people.DeleteResult(com_id);     break;    }  

 case 5:{     String[] s=new String[3];     System.out.println("根据职员编号更新其他字段信息,先输、");     for(int i=0;i<7;i++){      s[i]=scr.toString();     }     people.UpdateResult(s);     break;    }   }    num=scr.nextInt();  }      

 }

}

package cn.com;

import java.sql.*;

 public class Execute {  

private static String SourceName = null;  

 //数据库的字段数的输出

 public  void NumberResult(){  

 Connection con;   Statement stmt;  

 ResultSet rs;  

 String sql="select * from TableName";

  int row = 0;

  try{    

Class.forName("com.mysql.jdbc.Driver");  

  String url="jdbc:odbc://172.17.151.254:3306/"+SourceName;    con=DriverManager.getConnection(url, "root", "test");    stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);    rs=stmt.executeQuery(sql);    

   row=rs.getRow();   

 rs.close();   

 con.close();   }  

 catch(ClassNotFoundException e){    System.out.println("数据库连接错误。"+e);   }   catch(SQLException e){    System.out.println("表名输入错误。"+e);   }      System.out.println(row);  }      

 //查询语句   public void SelectResult(String s){

  Connection con;  

 PreparedStatement stmt;  

 ResultSet rs;   

String sql="select * from TableName where com_id=?";

  try{    Class.forName("com.mysql.jdbc.Driverr");  

  String url="jdbc:odbc://172.17.151.254:3306/"+SourceName;    con=DriverManager.getConnection(url, "root", "test");    stmt=con.prepareStatement(sql);    stmt.setString(1, s);    rs=stmt.executeQuery();    System.out.println("请输出指定的公司职员情况:");    while(rs.next()){     String com_id=rs.getString(1);     String com_name=rs.getString(2);     String com_sex=rs.getString(3);     String com_position=rs.getString(4);     String com_telephone=rs.getString(5);     String com_phone=rs.getString(6);     String com_email=rs.getString(7);      System.out.println(com_id);     System.out.println(com_name);     System.out.println(com_sex);     System.out.println(com_position);     System.out.println(com_telephone);     System.out.println(com_phone);     System.out.println(com_email);    }     rs.close();     stmt.close();     con.close();   }   catch(ClassNotFoundException e){    System.out.println("数据库连接错误。"+e);   }   catch(SQLException e){    System.out.println("表名输入错误。"+e);   }  }    //数据库的插入操作  public Boolean InsertResult(String[] s){   Connection con;   PreparedStatement stmt;   Boolean judge=false;   String sql="insert into TableName values (?,?,?,?,?,?,?) ";   try{    Class.forName("com.mysql.jdbc.Driver");    String url="jdbc:odbc://172.17.151.254:3306/"+SourceName;    con=DriverManager.getConnection(url, "root", "test");    stmt=con.prepareStatement(sql);    for(int i=0;i<7;i++){     stmt.setString(i+1, s[i]);    }    judge=stmt.execute();    stmt.close();    con.close();    }   catch(ClassNotFoundException e){    System.out.println(""+e);   }   catch(SQLException e){    System.out.println(""+e);   }   return judge;  }    //数据库的删除操作  public Boolean DeleteResult(String s){   Connection con;   PreparedStatement stmt;   Boolean judge=false;   String sql="delete from TableName where com_id=? ";   try{    Class.forName("com.mysql.jdbc.Driver");    String url="jdbc:odbc://172.17.151.254:3306/"+SourceName;    con=DriverManager.getConnection(url, "root", "test");    stmt=con.prepareStatement(sql);    stmt.setString(1, s);    judge=stmt.execute();    stmt.close();    con.close();    }   catch(ClassNotFoundException e){    System.out.println(""+e);   }   catch(SQLException e){    System.out.println(""+e);   }   return judge;  }    //数据库的更新操作  public Boolean UpdateResult(String[] s){   Connection con;   PreparedStatement stmt;   Boolean judge=false;   String sql="update TableName set ?=?,where com_id=?";   try{    Class.forName("com.mysql.jdbc.Driver");    String url="jdbc:odbc://172.17.151.254:3306/"+SourceName;    con=DriverManager.getConnection(url, "root", "test");    stmt=con.prepareStatement(sql);    for(int i=0;i<3;i++){     stmt.setString(i+1, s[i]);    }    judge=stmt.execute();    stmt.close();    con.close();    }   catch(ClassNotFoundException e){    System.out.println(""+e);   }   catch(SQLException e){    System.out.println(""+e);   }   return judge;  }      public void setSourceName(String s){    SourceName = s;  }  public void setTableName(String s){   String TableName=s;  } }

问题补充:

【1】查询数据表的记录数
【2】数据库的查询操作
【3】数据库的插入操作
【4】数据库的删除操作
【5】数据库的更新操作
请选择你要进行的数据库操作序号:
1
数据库连接错误。java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
0

zsy1的主页 zsy1 | 初学一级 | 园豆:166
提问于:2015-06-25 19:39
< >
分享
最佳答案
1

java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver

其他我都看不懂,你也只需要贴一句就行了,Google 上面这句,你会找到答案的。

收获园豆:10
爱编程的大叔 | 高人七级 |园豆:30517 | 2015-06-25 23:43
其他回答(3)
0

坐等大叔来喷。建议先格式化一下代码,编辑器有这个功能,然后再说明下报的是什么错误

会长 | 园豆:7797 (大侠五级) | 2015-06-25 20:02
0

0 0  这位小哥, 为何不用插入代码功能~

今天正好有空, 为了方便大神回答, 我帮你把代码整理了一下:

首先是Execute.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Execute {
    private static String SourceName = null;

    // 数据库的字段数的输出
    public void NumberResult() {
        Connection con;
        Statement stmt;
        ResultSet rs;
        String sql = "select * from TableName";
        int row = 0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:odbc://172.17.151.254:3306/" + SourceName;
            con = DriverManager.getConnection(url, "root", "test");
            stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            rs = stmt.executeQuery(sql);
            row = rs.getRow();
            rs.close();
            con.close();
        } catch (ClassNotFoundException e) {
            System.out.println("数据库连接错误。" + e);
        } catch (SQLException e) {
            System.out.println("表名输入错误。" + e);
        }
        System.out.println(row);
    }

    // 查询语句
    public void SelectResult(String s) {

        Connection con;
        PreparedStatement stmt;
        ResultSet rs;
        String sql = "select * from TableName where com_id=?";

        try {
            Class.forName("com.mysql.jdbc.Driverr");
            String url = "jdbc:odbc://172.17.151.254:3306/" + SourceName;
            con = DriverManager.getConnection(url, "root", "test");
            stmt = con.prepareStatement(sql);
            stmt.setString(1, s);
            rs = stmt.executeQuery();
            System.out.println("请输出指定的公司职员情况:");

            while (rs.next()) {
                String com_id = rs.getString(1);
                String com_name = rs.getString(2);
                String com_sex = rs.getString(3);
                String com_position = rs.getString(4);
                String com_telephone = rs.getString(5);
                String com_phone = rs.getString(6);
                String com_email = rs.getString(7);
                System.out.println(com_id);
                System.out.println(com_name);
                System.out.println(com_sex);
                System.out.println(com_position);
                System.out.println(com_telephone);
                System.out.println(com_phone);
                System.out.println(com_email);

            }
            rs.close();
            stmt.close();
            con.close();

        } catch (ClassNotFoundException e) {
            System.out.println("数据库连接错误。" + e);
        } catch (SQLException e) {
            System.out.println("表名输入错误。" + e);
        }
    }

    // 数据库的插入操作
    public Boolean InsertResult(String[] s) {
        Connection con;
        PreparedStatement stmt;
        Boolean judge = false;
        String sql = "insert into TableName values (?,?,?,?,?,?,?) ";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:odbc://172.17.151.254:3306/" + SourceName;
            con = DriverManager.getConnection(url, "root", "test");
            stmt = con.prepareStatement(sql);
            for (int i = 0; i < 7; i++) {
                stmt.setString(i + 1, s[i]);
            }
            judge = stmt.execute();
            stmt.close();
            con.close();
        } catch (ClassNotFoundException e) {
            System.out.println("" + e);
        } catch (SQLException e) {
            System.out.println("" + e);
        }
        return judge;
    } // 数据库的删除操作

    public Boolean DeleteResult(String s) {
        Connection con;
        PreparedStatement stmt;
        Boolean judge = false;
        String sql = "delete from TableName where com_id=? ";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:odbc://172.17.151.254:3306/" + SourceName;
            con = DriverManager.getConnection(url, "root", "test");
            stmt = con.prepareStatement(sql);
            stmt.setString(1, s);
            judge = stmt.execute();
            stmt.close();
            con.close();
        } catch (ClassNotFoundException e) {
            System.out.println("" + e);
        } catch (SQLException e) {
            System.out.println("" + e);
        }
        return judge;
    } // 数据库的更新操作

    public Boolean UpdateResult(String[] s) {
        Connection con;
        PreparedStatement stmt;
        Boolean judge = false;
        String sql = "update TableName set ?=?,where com_id=?";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:odbc://172.17.151.254:3306/" + SourceName;
            con = DriverManager.getConnection(url, "root", "test");
            stmt = con.prepareStatement(sql);
            for (int i = 0; i < 3; i++) {
                stmt.setString(i + 1, s[i]);
            }
            judge = stmt.execute();
            stmt.close();
            con.close();
        } catch (ClassNotFoundException e) {
            System.out.println("" + e);
        } catch (SQLException e) {
            System.out.println("" + e);
        }
        return judge;
    }

    public void setSourceName(String s) {
        SourceName = s;
    }

    public void setTableName(String s) {
        String TableName = s;
    }
}

然后是 Company.java

import java.util.*;

public class Company {
    private static Scanner scr;

    public static void main(String[] args) {
        Execute people;
        people = new Execute();
        people.setSourceName("Company");
        people.setTableName("Company_Employees");
        System.out.println("【1】查询数据表的记录数");
        System.out.println("【2】数据库的查询操作");
        System.out.println("【3】数据库的插入操作");
        System.out.println("【4】数据库的删除操作");
        System.out.println("【5】数据库的更新操作");
        System.out.println("请选择你要进行的数据库操作序号:");
        scr = new Scanner(System.in);
        int num = scr.nextInt();
        while (num != 0) {
            switch (num) {
                case 1: {
                    people.NumberResult();
                    break;
                }
                case 2: {
                    System.out.println("请输入要进行查询的公司员工的编号:");
                    String com_id = scr.toString();
                    people.SelectResult(com_id);
                    break;
                }
                case 3: {
                    String[] s = new String[7];
                    System.out.println("依次输入一条记录的信息:");
                    for (int i = 0; i < 7; i++) {
                        s[i] = scr.toString();
                    }
                    people.InsertResult(s);
                    break;
                }
                case 4: {
                    System.out.println("请输入要进行删除记录的公司员工的编号:");
                    String com_id = scr.toString();
                    people.DeleteResult(com_id);
                    break;
                }
                case 5: {
                    String[] s = new String[3];
                    System.out.println("根据职员编号更新其他字段信息,先输、");
                    for (int i = 0; i < 7; i++) {
                        s[i] = scr.toString();
                    }
                    people.UpdateResult(s);
                    break;
                }
            }
            num = scr.nextInt();
        }
    }
}
收获园豆:5
蒋至乙 | 园豆:288 (菜鸟二级) | 2015-06-25 20:07

谢谢

支持(0) 反对(0) zsy1 | 园豆:166 (初学一级) | 2015-06-25 21:02

【1】查询数据表的记录数
【2】数据库的查询操作
【3】数据库的插入操作
【4】数据库的删除操作
【5】数据库的更新操作
请选择你要进行的数据库操作序号:
1
数据库连接错误。java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
0

支持(0) 反对(0) zsy1 | 园豆:166 (初学一级) | 2015-06-25 21:03
1

楼上已经说明白了,就是没导入数据库的驱动包。

收获园豆:5
angelshelter | 园豆:9676 (大侠五级) | 2015-06-26 07:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册