求jsp连接mysql的完整代码及说明!
假设mysql下有一数据库叫student,表名称叫st_info,含有两个字段,名为name及sex,求教如何连接并读取st_info中的数据。...
假设mysql下有一数据库叫student,表名称叫st_info,含有两个字段,名为name及sex,求教如何连接并读取st_info中的数据。
展开
1个回答
展开全部
我给你 后台通用的增删改 方法 , sql 语句我就写了,自己去调用吧
<%
private static Connection con = null;
private static String CONSTR = "jdbc:mysql://localhost:3306/student";
private static String CLASSNAME = "com.mysql.jdbc.Driver";
private static PreparedStatement prst = null;
private static ResultSet rs = null;
/**
* @param args
* @throws SQLException
* @throws ClassNotFoundException
* @throws ClassNotFoundException
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
/**
* 获得连接
* @return
* @throws ClassNotFoundException
* @throws SQLException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public Connection getConnection(){
try {
Class.forName(CLASSNAME).newInstance();//加载驱动
con = DriverManager.getConnection(CONSTR, "数据库用户名","密码");//得connection
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
/**
* 增、删、改
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
*/
public void insertUpdate(String sql,Object...param){
getConnection();
try {
prst = con.prepareStatement(sql);
bindParam(param);
prst.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 查找方法
* @param sql
* @param param
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
*/
public ResultSet search(String sql,Object...param){
getConnection();
try {
prst = con.prepareStatement(sql);
bindParam(param);
rs = prst.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
/**
* 绑定参数
* @param param
* @throws SQLException
*/
private void bindParam(Object... param) throws SQLException {
if(param!=null){
for(int i=0;i<param.length;i++){
if(param[i] instanceof java.util.Date){
java.util.Date dd = (java.util.Date)param[i];
java.sql.Date sd = new java.sql.Date(dd.getTime());
prst.setDate(i,sd);
}else{
prst.setObject(i,param[i]);
}
}
}
}
/**
* 释放资源
* @throws SQLException
*/
public void realse(ResultSet rs) throws SQLException{
if(rs!=null){
rs.close();
}
if(prst!=null){
prst.close();
}
if(con!=null){
con.close();
}
}
public void realse() throws SQLException{
realse(null);
}
%>
<%
private static Connection con = null;
private static String CONSTR = "jdbc:mysql://localhost:3306/student";
private static String CLASSNAME = "com.mysql.jdbc.Driver";
private static PreparedStatement prst = null;
private static ResultSet rs = null;
/**
* @param args
* @throws SQLException
* @throws ClassNotFoundException
* @throws ClassNotFoundException
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
*/
/**
* 获得连接
* @return
* @throws ClassNotFoundException
* @throws SQLException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public Connection getConnection(){
try {
Class.forName(CLASSNAME).newInstance();//加载驱动
con = DriverManager.getConnection(CONSTR, "数据库用户名","密码");//得connection
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
/**
* 增、删、改
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
*/
public void insertUpdate(String sql,Object...param){
getConnection();
try {
prst = con.prepareStatement(sql);
bindParam(param);
prst.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 查找方法
* @param sql
* @param param
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws ClassNotFoundException
*/
public ResultSet search(String sql,Object...param){
getConnection();
try {
prst = con.prepareStatement(sql);
bindParam(param);
rs = prst.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
/**
* 绑定参数
* @param param
* @throws SQLException
*/
private void bindParam(Object... param) throws SQLException {
if(param!=null){
for(int i=0;i<param.length;i++){
if(param[i] instanceof java.util.Date){
java.util.Date dd = (java.util.Date)param[i];
java.sql.Date sd = new java.sql.Date(dd.getTime());
prst.setDate(i,sd);
}else{
prst.setObject(i,param[i]);
}
}
}
}
/**
* 释放资源
* @throws SQLException
*/
public void realse(ResultSet rs) throws SQLException{
if(rs!=null){
rs.close();
}
if(prst!=null){
prst.close();
}
if(con!=null){
con.close();
}
}
public void realse() throws SQLException{
realse(null);
}
%>
网易云信
2023-12-06 广告
2023-12-06 广告
UIkit是一套轻量级、模块化且易于使用的开源UI组件库,由YOOtheme团队开发。它提供了丰富的界面元素,包括按钮、表单、表格、对话框、滑块、下拉菜单、选项卡等等,适用于各种类型的网站和应用程序。UIkit还支持响应式设计,可以根据不同...
点击进入详情页
本回答由网易云信提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询