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
java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
其他我都看不懂,你也只需要贴一句就行了,Google 上面这句,你会找到答案的。
坐等大叔来喷。建议先格式化一下代码,编辑器有这个功能,然后再说明下报的是什么错误
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(); } } }
谢谢
【1】查询数据表的记录数
【2】数据库的查询操作
【3】数据库的插入操作
【4】数据库的删除操作
【5】数据库的更新操作
请选择你要进行的数据库操作序号:
1
数据库连接错误。java.lang.ClassNotFoundException: sun.jdbc.odbc.JdbcOdbcDriver
0
楼上已经说明白了,就是没导入数据库的驱动包。