本文目錄一覽:
求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數據庫中的數據
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/248473.html