connio.java
来自「成绩查询系统(学校期末的作业) Struts+jdbc+mysql+tomca」· Java 代码 · 共 96 行
JAVA
96 行
package sfs.connection;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public abstract class ConnIO{
public Connection conn;
//连接数据库
public ConnIO() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost/my_score?useUnicode=true&characterEncoding=GB2312";
String name="root";
String psw="root123";
conn = (Connection) DriverManager.getConnection(url,name,psw);
}
//查询
public List Query(String sql,String table) throws SQLException{
List list=new ArrayList();
ResultSet res;
res = conn.createStatement().executeQuery(sql);
if(res.next()){
res.beforeFirst();
while(res.next()){
list.add(ResToObj.excute(table,res));
}
res.close();
return list;
}
return null;
}
public List QueryByNumber(String table,Object obj) throws SQLException, NumberFormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
ObjToRes otr=new ObjToRes(table,obj);
String sql=null;
String number=otr.getString("number");
sql="select * from "+table+" where number="+number;
List list=new ArrayList();
ResultSet res;
res = conn.createStatement().executeQuery(sql);
if(res.next()){
res.beforeFirst();
while(res.next()){
list.add(ResToObj.excute(table,res));
}
res.close();
return list;
}
return null;
}
public List QueryById(String table,Object obj) throws SQLException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
ObjToRes otr=new ObjToRes(table,obj);
String sql=null;
int id=otr.getInt("id");
sql="select * from "+table+" where id="+id;
List list=new ArrayList();
ResultSet res;
res = conn.createStatement().executeQuery(sql);
if(res.next()){
res.beforeFirst();
while(res.next()){
list.add(ResToObj.excute(table,res));
}
res.close();
return list;
}
return null;
}
//删除
public void DeletById(String table,Object obj) throws SQLException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
ObjToRes otr=new ObjToRes(table,obj);
String sql = null; //初始化sql
int id=otr.getInt("id");
sql="delete from "+table+" where id="+id;
conn.createStatement().execute(sql);
}
public void DeletByNumber(String table,Object obj) throws SQLException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
ObjToRes otr=new ObjToRes(table,obj);
String sql = null; //初始化sql
String number=otr.getString("number");
sql="delete from "+table+" where number="+number;
conn.createStatement().execute(sql);
}
//增加更新操作
public void SaveOrUpdate(String table,Object obj) throws SQLException, NumberFormatException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?