📄 todatabase.java
字号:
package bookmanager;
import java.sql.*;
import javax.swing.JOptionPane;
public class toDatabase {
/***************************************************************************
*** declaration of the private variables used in the program ***
***************************************************************************/
//for setting the connection and statement
JLibrary myMain;
private Connection connection = null;
private Statement statement = null;
private ResultSet resultset = null;
public boolean isDouble(String query) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ea) {
System.out.println(ea.toString());
}
catch(Exception e) {
System.out.println(e.toString());
}
/***************************************************************
* for making the connection,creating the statement and update *
* the table in the database. After that,closing the statmenet *
* and connection. There is catch block SQLException for error *
***************************************************************/
try {
connection = DriverManager.getConnection("jdbc:odbc:JLibrary");
statement = connection.createStatement();
resultset = statement.executeQuery(query);
while(resultset.next()) {
return true;
}
resultset.close();
statement.close();
connection.close();
}
catch(SQLException SQLe) {
System.out.println(SQLe.toString());
}
return false;
}
public int numberOfBooks(String query) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ea) {
System.out.println(ea.toString());
}
catch(Exception e) {
System.out.println(e.toString());
}
/***************************************************************
* for making the connection,creating the statement and update *
* the table in the database. After that,closing the statmenet *
* and connection. There is catch block SQLException for error *
***************************************************************/
try {
connection = DriverManager.getConnection("jdbc:odbc:JLibrary");
statement = connection.createStatement();
resultset = statement.executeQuery(query);
while(resultset.next()) {
return resultset.getInt("NumberOfBooks");
}
resultset.close();
statement.close();
connection.close();
}
catch(SQLException SQLe) {
System.out.println(SQLe.toString());
}
return -1;
}
public int memberID(String query) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ea) {
System.out.println(ea.toString());
}
catch(Exception e) {
System.out.println(e.toString());
}
/***************************************************************
* for making the connection,creating the statement and update *
* the table in the database. After that,closing the statmenet *
* and connection. There is catch block SQLException for error *
***************************************************************/
try {
connection = DriverManager.getConnection("jdbc:odbc:JLibrary");
statement = connection.createStatement();
resultset = statement.executeQuery(query);
while(resultset.next()) {
return resultset.getInt("MemberID");
}
resultset.close();
statement.close();
connection.close();
}
catch(SQLException SQLe) {
System.out.println(SQLe.toString());
}
return -1;
}
public void update(String query) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ea) {
System.out.println(ea.toString());
}
catch(Exception e) {
System.out.println(e.toString());
}
/***************************************************************
* for making the connection,creating the statement and update *
* the table in the database. After that,closing the statmenet *
* and connection. There is catch block SQLException for error *
***************************************************************/
try {
connection = DriverManager.getConnection("jdbc:odbc:JLibrary");
statement = connection.createStatement();
statement.executeUpdate(query);
statement.close();
connection.close();
}
catch(SQLException SQLe) {
System.out.println(SQLe.toString());
}
}
public String isVerityUser(String query){
String Leveled="";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
}
catch(ClassNotFoundException ea) {
System.out.println(ea.toString());
}
catch(Exception e) {
System.out.println(e.toString());
}
/***************************************************************
* for making the connection,creating the statement and update *
* the table in the database. After that,closing the statement *
* and connection. There is catch block SQLException for error *
***************************************************************/
try {
connection = DriverManager.getConnection("jdbc:odbc:JLibrary");
statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs;
rs=statement.executeQuery(query);
boolean isexist = false;
try {
isexist = rs.first();
}
catch (SQLException ex1) {
}
//若用户名不存在,提示警告信息
if (!isexist) {
JOptionPane.showMessageDialog(null, "Login error!");
myMain.setEnable("else");
}
else {JOptionPane.showMessageDialog(null, "login successly!");
try {
rs.first();
Leveled= rs.getString("Level").trim();
}
catch (SQLException ex) {
}
statement.close();
connection.close();
return Leveled;
}
}
catch(SQLException SQLe) {
System.out.println(SQLe.toString());
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -