⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mydb.java

📁 自己写的一个网站,学生管理系,还有带自己写开发文档,可以用于高校学生管理,具体见文档
💻 JAVA
字号:
package DB;

import java.sql.*;
public class MyDB{

		private static String driverName;
		private String dbURL ;
		private String userName ;
		private String userPwd;
		private Connection Conn;
		private ResultSet result;
		private Statement sql;

		public   void getConnect(){						//构造函数
			dbURL = "jdbc:odbc:student";		//university为数据源
			driverName = "sun.jdbc.odbc.JdbcOdbcDriver";	//设置驱动程序
			userName = "";
			userPwd = "";
			result=null;
			try{
					Class.forName(driverName);
					Conn = DriverManager.getConnection(dbURL, userName, userPwd);
					sql = Conn.createStatement();
					//return dbConn;
			}

			catch(Exception e) {
					e.printStackTrace();
					//return null;
			}
		}

		public  Connection getConn(){
				return Conn;
			}

		public  void getConnect(String dataSource,String userName,String userPwd){
			this.dbURL="jdbc:odbc:"+dataSource;
			driverName = "sun.jdbc.odbc.JdbcOdbcDriver";	//设置驱动程序
			this.userName=userName;
			this.userPwd=userPwd;
			result=null;
			try{
					Class.forName(driverName);
					Conn = DriverManager.getConnection(dbURL, userName, userPwd);
					sql = Conn.createStatement();
					//return sql;
			}

			catch(Exception e) {
				e.printStackTrace();
				//return null;
			}
		}

		public ResultSet queryTable(String tableName){		//查找表名为的信息,该表的列数不清为numOfColumn
			try{
					String query = "select * from ";
					query=query+tableName;
					result = sql.executeQuery(query);
					return result;

			}
			catch(Exception e) {
					e.printStackTrace();
					return null;
			}
		}

		public Statement getSql(){
			return sql;
		}

		public boolean queryRecord(String tableName,int studentID){			//在相应表(tableName)中查找相应是否有学生学号为(studentID)的学生

			try{
				String query = "select * from ";
				query=query+tableName;
				result = sql.executeQuery(query);
			}
			catch(Exception e){
				e.printStackTrace();
			}

			try{
				while(result.next()){
					int tempID=result.getInt(1);
					if(tempID==studentID)
						return true;				//找查到学号是studentID的学生
				}
			}
			catch(SQLException ex){
				System.err.println("SQLException:" + ex.getMessage());
			}
			return false;							//没找到该学生的
		}

		public boolean getUser(String tableName,String userid,String pwd){

			try{
					String query = "select * from ";
					query=query+tableName;
					result = sql.executeQuery(query);
				}
			catch(Exception e){
					e.printStackTrace();
			}

			try{
					while(result.next()){
						String tempID=result.getString(1);
						String tempPwd=result.getString(2);
						if((userid==tempID)&&(pwd==tempPwd))
								return true;				//找查到学号是studentID的学生
					}
			}

			catch(SQLException ex){
					System.err.println("SQLException:" + ex.getMessage());
			}

			return false;							//没找到该学生的
		}

		public boolean insertRecord(int id,String name,String sex,int age,String address,String department){		//往相应表中插入一条记录,列序列分别为:学号,姓名,性别,年龄,地址,学院

			String insert="insert into studentInformation(id,name,sex,age,address,department)values('"+id+"','"+name+"','"+sex+"','"+age+"','"+address+"','"+department+"')"; ;

			if((queryRecord("studentInformation",id))==true)
				return false;

			try{
					sql.executeUpdate(insert);
					sql.close();
					return true;
			}
			catch(SQLException e){
				e.printStackTrace();
				return false;
			}
		}

		public boolean insertAdmin(String id,String pwd){		//往相应表中插入一条记录,列序列分别为:学号,姓名,性别,年龄,地址,学院

					String insert="insert into userpassword(USER_ID,USER_PASSWORD)values('"+id+"','"+pwd+"')";

					try{
							sql.executeUpdate(insert);
							sql.close();
							return true;
					}
					catch(SQLException e){
						e.printStackTrace();
						return false;
					}
		}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -