首页 > 编程知识 正文

jspmysql源代码(jsp源码之家)

时间:2023-12-24 12:05:35 阅读:320606 作者:OVWQ

本文目录一览:

求jsp与mysql连接的代码

import java.sql.*;

public class DbOperator {

public final String DB_USER= "root";

public final String DB_PWD = "1234";

public final String DB_HOST = "127.0.0.1";

public final String DB_NAME = "test";

public DbOperator() {

}

/**

* 得到数据库连接

* @return Connection

*/

public Connection getConnection()

{

Connection conn = null;

String url = "jdbc:mysql://"+this.DB_HOST+"/"+this.DB_NAME+"?useUnicode=truecharacterEncoding=GBK";

try

{

Class.forName("com.mysql.jdbc.Driver").newInstance();

conn = java.sql.DriverManager.getConnection(url, this.DB_USER, this.DB_PWD);

}catch(Exception e)

{

e.printStackTrace();

}

return conn;

}

}

使用的

Connection conn = dbOperator.getConnection();

try

{

Statement st = conn.createStatement();

String sql = " select * from user where username ='" + username + "' and pwd ='" + pwd + "'";

ResultSet rs = st.executeQuery(sql);

if(rs.next())

{

userInfo = new UserInfo();

userInfo.setAge(rs.getString("age"));

userInfo.setId(rs.getInt("id"));

userInfo.setPwd(rs.getString("pwd"));

userInfo.setSex(rs.getString("sex"));

userInfo.setTheName(rs.getString("the_name"));

userInfo.setUserName(rs.getString("username"));

}

rs.close();

st.close();

conn.close();

}catch(Exception e)

{

e.printStackTrace();

}

return userInfo;

jsp+mysql源代码怎么用

一般下载的源码,自己可以直接阅读,如果是以某些文件的工程文件打包的,可以自己下载相应IDE环境来打开操作,比如myeclipse,自己下载源码后,可以导入MyEclipse中,然后把sql文件导入mysql中,生成本地的数据库,接着找到项目中配置数据库连接的java类,把数据库的用户名和密码改成你自己的数据库的用户名和密码(这步很重要),最后把项目部署到tomcat中,启动tomcat服务器,就可以运行了

求JSP向mysql插入,删除,查询数据源代码

还有这样的需求呀?无奇不有……

写的dao的实现就行了呀 ,何必得要jsp呢?用jsp,只会越来越乱^

CustomerDao.java

package cn.itcast.dao;

import java.util.List;

import cn.itcast.domain.Customer;

public interface CustomerDao {

public void add(Customer customer);

public Customer find(int id);

public List getAllCustomer();

public void delete(int id);

public void update(Customer customer);

public int getAllRecord();

public List getCustomerByPage(int startindex,int pagesize);

}

CustomerDaoJdbcImpl.java

package cn.itcast.dao.impl;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;

import cn.itcast.dao.CustomerDao;

import cn.itcast.domain.Customer;

import cn.itcast.util.JdbcUtils;

public class CustomerDaoJdbcImpl implements CustomerDao {

/*

id int primary key auto_increment,

name varchar(20) not null,

sex varchar(4) not null,

birthday date,

cellphone varchar(20) not null,

Email varchar(40),

preference varchar(100),

type varchar(40),

Description varchar(255)

*/

public void add(Customer customer) {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtils.getConnection();

String sql = "insert into customer(name,sex,birthday,cellphone,email,preference,type,description) values(?,?,?,?,?,?,?,?)";

st = conn.prepareStatement(sql);

st.setString(1, customer.getName());

st.setString(2, customer.getSex());

st.setDate(3, new java.sql.Date(customer.getBirthday().getTime()));

st.setString(4, customer.getCellphone());

st.setString(5, customer.getEmail());

st.setString(6, customer.getPreference());

st.setString(7, customer.getType());

st.setString(8, customer.getDescription());

st.executeUpdate();

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtils.release(rs, st, conn);

}

}

public void delete(int id) {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtils.getConnection();

String sql = "delete from customer where id=?";

st = conn.prepareStatement(sql);

st.setInt(1, id);

st.executeUpdate();

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtils.release(rs, st, conn);

}

}

public Customer find(int id) {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtils.getConnection();

String sql = "select id,name,sex,birthday,cellphone,email,preference,type,description from customer where id=?";

st = conn.prepareStatement(sql);

st.setInt(1, id);

rs = st.executeQuery();

if(rs.next()){

Customer c = new Customer();

c.setId(rs.getInt("id"));

c.setName(rs.getString("name"));

c.setSex(rs.getString("sex"));

c.setBirthday(rs.getDate("birthday"));

c.setCellphone(rs.getString("cellphone"));

c.setEmail(rs.getString("email"));

c.setPreference(rs.getString("preference"));

c.setType(rs.getString("type"));

c.setDescription(rs.getString("description"));

return c;

}

return null;

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtils.release(rs, st, conn);

}

}

/*

Id 编号 varchar(20)

name 客户姓名 varchar(20)

sex 性名 varchar(4)

birthday 生日 date

cellphone 手机 varchar(20)

Email 电子邮件 varchar(40)

preference 客户爱好 varchar(100)

type 客户类型 varchar(40)

Description 备注 varchar(255)

*/

public List getAllCustomer() {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtils.getConnection();

String sql = "select id,name,sex,birthday,cellphone,email,preference,type,description from customer order by id";

st = conn.prepareStatement(sql);

rs = st.executeQuery();

List list = new ArrayList();

while(rs.next()){

Customer c = new Customer();

c.setId(rs.getInt("id"));

c.setName(rs.getString("name"));

c.setSex(rs.getString("sex"));

c.setBirthday(rs.getDate("birthday"));

c.setCellphone(rs.getString("cellphone"));

c.setEmail(rs.getString("email"));

c.setPreference(rs.getString("preference"));

c.setType(rs.getString("type"));

c.setDescription(rs.getString("description"));

list.add(c);

}

return list;

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtils.release(rs, st, conn);

}

}

public void update(Customer customer) {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtils.getConnection();

String sql = "update customer set name=?,sex=?,birthday=?,cellphone=?,email=?,preference=?,type=?,description=? where id=?";

st = conn.prepareStatement(sql);

st.setString(1, customer.getName());

st.setString(2, customer.getSex());

st.setDate(3, new java.sql.Date(customer.getBirthday().getTime()));

st.setString(4, customer.getCellphone());

st.setString(5, customer.getEmail());

st.setString(6, customer.getPreference());

st.setString(7, customer.getType());

st.setString(8, customer.getDescription());

st.setInt(9, customer.getId());

st.executeUpdate();

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtils.release(rs, st, conn);

}

}

public int getAllRecord() {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtils.getConnection();

String sql = "select count(*) from customer";

st = conn.prepareStatement(sql);

rs = st.executeQuery();

if(rs.next()){

return rs.getInt(1);

}

return 0;

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtils.release(rs, st, conn);

}

}

public List getCustomerByPage(int startindex, int pagesize) {

Connection conn = null;

PreparedStatement st = null;

ResultSet rs = null;

try{

conn = JdbcUtils.getConnection();

String sql = "select id,name,sex,birthday,cellphone,email,preference,type,description from customer limit ?,?";

st = conn.prepareStatement(sql);

st.setInt(1, startindex);

st.setInt(2, pagesize);

rs = st.executeQuery();

List list = new ArrayList();

while(rs.next()){

Customer c = new Customer();

c.setId(rs.getInt("id"));

c.setName(rs.getString("name"));

c.setSex(rs.getString("sex"));

c.setBirthday(rs.getDate("birthday"));

c.setCellphone(rs.getString("cellphone"));

c.setEmail(rs.getString("email"));

c.setPreference(rs.getString("preference"));

c.setType(rs.getString("type"));

c.setDescription(rs.getString("description"));

list.add(c);

}

return list;

}catch(Exception e){

throw new RuntimeException(e);

}finally{

JdbcUtils.release(rs, st, conn);

}

}

}

jsp怎么连接mysql数据库代码

jsp连接mysql数据库的操作方式。

1、在数据服务端安装好mysql数据库,这个是必须的,在自己的ssh或者虚拟机上,数据mysql可以看到相关的提示,说明安装成功

2、我是用的是tomcat服务器,在这里需要安装好java连接mysql的数据库操作库。我是用的jar包是:mysql-connector-java-3.1.14.tar.gz,大家可以在网上下载,或者,在官网上下载

3、把解包后的jar放到tomcat里面的lib文件夹下

4、在程序的代码段里添加连接函数库和库函数,就可以连接到mysql数据库了

5、剩下的就是我们使用的时候调用这样的数据了,在jsp里使用mysql数据库中的数据

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。