📄 dboperations.java
字号:
package com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBOperations
{
private Connection connection = null;
private String driver = "";
private String password = "";
private ResultSet resultset = null;
private String source = "";
private Statement statemet = null;
private String username = "";
public DBOperations()
{
setDbParameters();
}
/**
* @return Returns the driver.
*/
public String getDriver()
{
return driver;
}
/**
* @return Returns the password.
*/
public String getPassword()
{
return password;
}
/**
* @return Returns the source.
*/
public String getSource()
{
return source;
}
/**
* @return Returns the username.
*/
public String getUsername()
{
return username;
}
/**
* @param driver The driver to set.
*/
public void setDriver(String driver)
{
this.driver = driver;
}
/**
* @param password The password to set.
*/
public void setPassword(String password)
{
this.password = password;
}
/**
* @param source The source to set.
*/
public void setSource(String source)
{
this.source = source;
}
/**
* @param username The username to set.
*/
public void setUsername(String username)
{
this.username = username;
}
public Connection getConnection()
{
try
{
Class.forName( driver );
connection = DriverManager.getConnection( source, username,password );
} catch (ClassNotFoundException e)
{
e.printStackTrace();
} catch (SQLException e)
{
e.printStackTrace();
}
return connection;
}
public void setDbParameters()
{
String user="";
String pass="";
setUsername(user);
setPassword(pass);
setDriver("sun.jdbc.odbc.JdbcOdbcDriver");
setSource("jdbc:odbc:webDB");
try
{
connection=getConnection();
if(connection!=null)
{
System.out.println("Successfully Connected to the Database ! :"+connection.toString());
}
} catch (Exception e)
{
e.printStackTrace();
}
}
public int deleteFromTable(String deleteQuery)
{
int count=0;
try
{
statemet = connection.createStatement();
count=statemet.executeUpdate( deleteQuery );
} catch (SQLException e)
{
e.printStackTrace();
return count;
}
return count;
}
public ResultSet selectFromTable(String selectQuery)
{
try
{
statemet = connection.createStatement();
resultset=statemet.executeQuery( selectQuery );
return resultset;
} catch (SQLException e)
{
e.printStackTrace();
}
return null;
}
public int insertToTable(String insertQuery)
{
int res=0;
try
{
statemet = connection.createStatement();
res=statemet.executeUpdate( insertQuery );
if(res>=1)
{
return res;
}
} catch (SQLException e)
{
e.printStackTrace();
}
return res;
}
/**
* @return Returns the resultset.
*/
public ResultSet getResultset()
{
if(resultset!=null)
{
return resultset;
}
return null;
}
public void disConnect()
{
try
{
if(resultset!=null)
{
resultset.close();
}
if(statemet!=null)
{
statemet.close();
}
if(connection!=null)
{
connection.close();
}
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Select Count(*) From tblExam;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -