添加信息java代码 java添加用户信息

用JAVA对数据库信息进行增加、删除、修改、查看怎么写谁能帮忙,能的加QQ1505209873我把我写好的代码发给

这个不是简单的数据库 数据的添加 删除 修改 和查看吗

创新互联公司主营宁波网站建设的网络公司,主营网站建设方案,重庆App定制开发,宁波h5成都微信小程序搭建,宁波网站营销推广欢迎宁波等地区企业咨询

这个是一个家具买卖页面所涉及的 曾删改查都有了,详细内容看代码

package Dao;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.ArrayList;

import java.util.List;

import Entity.JIAJU;

public class JiaJu {

public JIAJU selectExe(int shouhinId) {

JIAJU jia = new JIAJU();

try {

Connection con = ConnectionManager.getConnection();

String sql = "select * from jiaju where shouhinId=?";

PreparedStatement ps = con.prepareStatement(sql);

ps.setInt(1, shouhinId);

ResultSet rs = ps.executeQuery();

if (rs != null) {

while (rs.next()) {

jia.setShouhinId(rs.getInt("shouhinId"));

jia.setShouhinName(rs.getString("shouhinName"));

jia.setShouhinColor(rs.getString("shouhinColor"));

jia.setShouhinPrice(rs.getInt("shouhinPrice"));

jia.setShouhinPai(rs.getString("shouhinPai"));

jia.setShouhinShi(rs.getString("shouhinShi"));

// list.add(jia);

}

}

} catch (Exception e) {

e.printStackTrace();

}

return jia;

}

public void insertJia(JIAJU jia) {

try {

Connection con = ConnectionManager.getConnection();

String sql = "insert into jiaju values(?,?,?,?,?)";

PreparedStatement ps = con.prepareStatement(sql);

ps.setString(1, jia.getShouhinName());

ps.setString(2, jia.getShouhinColor());

ps.setInt(3, jia.getShouhinPrice());

ps.setString(4, jia.getShouhinPai());

ps.setString(5, jia.getShouhinShi());

ps.executeUpdate();

} catch (Exception e) {

e.printStackTrace();

}

}

public List selectJia() {

List list = new ArrayList();

try {

Connection con = ConnectionManager.getConnection();

String sql = "select * from jiaju ";

PreparedStatement ps = con.prepareStatement(sql);

ResultSet rs = ps.executeQuery();

if (rs != null) {

while (rs.next()) {

JIAJU jia = new JIAJU();

jia.setShouhinId(rs.getInt("shouhinId"));

jia.setShouhinName(rs.getString("shouhinName"));

jia.setShouhinColor(rs.getString("shouhinColor"));

jia.setShouhinPrice(rs.getInt("shouhinPrice"));

jia.setShouhinPai(rs.getString("shouhinPai"));

jia.setShouhinShi(rs.getString("shouhinShi"));

list.add(jia);

}

}

} catch (Exception e) {

e.printStackTrace();

}

return list;

}

public JIAJU selectbuy(int shouhinId) {

JIAJU jia = new JIAJU();

try {

Connection con = ConnectionManager.getConnection();

String sql = "select * from jiaju where shouhinId=?";

PreparedStatement ps = con.prepareStatement(sql);

ps.setInt(1, shouhinId);

ResultSet rs = ps.executeQuery();

if (rs != null) {

while (rs.next()) {

jia.setShouhinId(rs.getInt("shouhinId"));

jia.setShouhinName(rs.getString("shouhinName"));

jia.setShouhinColor(rs.getString("shouhinColor"));

jia.setShouhinPrice(rs.getInt("shouhinPrice"));

jia.setShouhinPai(rs.getString("shouhinPai"));

jia.setShouhinShi(rs.getString("shouhinShi"));

}

}

} catch (Exception e) {

e.printStackTrace();

}

return jia;

}

public void updateLou(JIAJU jia){

try{

Connection con = ConnectionManager.getConnection();

String sql = "update jiaju set shouhinPrice=? where shouhinId=?";

PreparedStatement ps = con.prepareStatement(sql);

ps.setInt(1,jia.getShouhinPrice());

ps.setInt(2, jia.getShouhinId());

ps.executeUpdate();

}catch(Exception e){

e.printStackTrace();

}

}

public void deleteLou(JIAJU jia){

try{

Connection con = ConnectionManager.getConnection();

String sql = "delete from jiaju where shouhinId=?";

PreparedStatement ps = con.prepareStatement(sql);

ps.setInt(1, jia.getShouhinId());

ps.executeUpdate();

}catch(Exception e){

e.printStackTrace();

}

}

}

Java使用字符串生成器来保存购物车中的商品信息,并能实现商品信息的添加、删除以及修改等功能

public class Cart {

public StringBuilder data;

public float total;

public Cart(){

data=new StringBuilder();

}

public void buy(Goods g){

g.gtotal=g.gnum*g.gprice;

total=total+g.gtotal;

data.append("[");

data.append(g.gname+"|");

data.append(g.gprice+"|");

data.append(g.gnum+"|");  //还是竖线看着方便

data.append(g.gtotal);

data.append("]");

}

public void delete(Goods g){

int s=data.indexOf(g.gname);

int e=data.indexOf("]", s);

data.delete(s-1, e+1);

total=total-g.gtotal;  //删除商品 ,需要修改总额

}

public void update(Goods g){

data.replace(3, 10, "["+g.gname+"|"+g.gprice+"|"+g.gnum+"|"+g.gtotal);

}

public void show(){

System.out.print("总计金额:" + total + "    ") ;

System.out.println(data);

}

}

//Excute类里有点小错误,

//总觉得update方法 不对头,你想怎么做?

如何给JAVA源代码文件统一地添加licence信息头

1,你需要知道待操作的文件有哪几种,每种类型的头是怎么定义的,取多少位(例如BMP的文件头是 54 , 以下都是24 bit)

2,循环用java文件流类库,读取每个文件的前xx位,再根据1的定义判断修改文件后缀

大概用到这几个类:DataInputStream、BufferedInputStream、FileInputStream

求! 用JAVA编写一个通讯录,只要有最基本的添加、查找就可以

开发环境JBuilder2005 SQL server2003 SP3连接方士直链,如果直链不上改桥链,参考

数据文件代码:

create database userInfo

go

USE USERINFO

GO

create table uses(

username varchar(10) not null,

usertell varchar(20) not null,

usertells varchar(20) not null,

userQQ VARCHAR(20) NOT NULL,

USEREMAIL VARCHAR(20) NOT NULL,

USERADDRESS VARCHAR(20) NOT NULL

)

GO

Java部分代码:

文件一窗体控制类 文件名 Application1.java

package myjava;

import java.awt.Toolkit;

import javax.swing.SwingUtilities;

import javax.swing.UIManager;

import java.awt.Dimension;

public class Application1 {

boolean packFrame = false;

/**

* Construct and show the application.

*/

public Application1() {

Frame1 frame = new Frame1();

// Validate frames that have preset sizes

// Pack frames that have useful preferred size info, e.g. from their layout

if (packFrame) {

frame.pack();

} else {

frame.validate();

}

// Center the window

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

Dimension frameSize = frame.getSize();

if (frameSize.height screenSize.height) {

frameSize.height = screenSize.height;

}

if (frameSize.width screenSize.width) {

frameSize.width = screenSize.width;

}

frame.setLocation((screenSize.width - frameSize.width) / 2,

(screenSize.height - frameSize.height) / 2);

frame.setVisible(true);

}

/**

* Application entry point.

*

* @param args String[]

*/

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {

public void run() {

try {

UIManager.setLookAndFeel(UIManager.

getSystemLookAndFeelClassName());

} catch (Exception exception) {

exception.printStackTrace();

}

new Application1();

}

});

}

}

文件二 数据库驱动类文件名 DB.java

package myjava;

import java.sql.*;

/**

* pTitle: /p

*

* pDescription: /p

*

* pCopyright: Copyright (c) 2007/p

*

* pCompany: /p

*

* @author not attributable

* @version 1.0

*/

public class DB {

private String url = "jdbc:microsoft:sqlserver://";//SQLServer路径

private String serverName = "localhost";//服务器名称

private String portNumber = "1433";//端口号

private String databaseName = "userInfo";//数据库名称

private String userName = "sa";//登陆名

private String password = "sa";//密码

private Connection con = null;

private Statement st=null;

private ResultSet rs=null;

public DB(){

}

public void setUrl(String url){

this.url=url;

}

public String getUrl(){

return this.url;

}

public void setServerName(String serverName){

this.serverName=serverName;

}

public String getServerName(){

return this.serverName;

}

public void setPortNumber(String portNumber){

this.portNumber=portNumber;

}

public String getPortNumber(){

return this.portNumber;

}

public void setDatabaseName(String databaseName){

this.databaseName=databaseName;

}

public String getdatabaseName(){

return this.databaseName;

}

public void setUserName(String userName){

this.userName=userName;

}

public String getUserName(){

return this.userName;

}

public void setUserPassword(String password){

this.password=password;

}

public String getPassword(){

return this.password;

}

//返回连接url

private String getConnectionUrl(){

return url + serverName + ":" + portNumber + ";databaseName=" +

databaseName + ";";

}

//获得数据库联接驱动

public Connection getConnection(){

try {

//加载驱动

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

} catch (ClassNotFoundException ex) {

ex.printStackTrace();

return null;

}

try {

//获得驱动对象

con = DriverManager.

getConnection(getConnectionUrl(), userName,password);

} catch (SQLException ex1) {

ex1.printStackTrace();

return null;

}

return con;

}

//获得连接对象

public Statement getStatement(){

try {

st = getConnection().createStatement();

} catch (SQLException ex) {

ex.printStackTrace();

return null;

}

return st;

}

//获得结果集

public ResultSet getResultSet(String sql){

try {

rs = getStatement().executeQuery(sql);

} catch (SQLException ex) {

ex.printStackTrace();

return null;

}

return rs;

}

//更新数据库信息

public int getUpdate(String sql){

try {

return getStatement().executeUpdate(sql);

} catch (SQLException ex) {

ex.printStackTrace();

return 0;

}

}

//数据关闭驱动对象

public void close(){

try {

con.close();

} catch (SQLException ex) {

ex.printStackTrace();

}

}

//关闭连接对象

public void closeStatement(){

try {

if(st!=null){st.close();}

} catch (SQLException ex) {

ex.printStackTrace();

}

}

//关闭结果集

public void closeRestultSet(){

if(rs!=null){

try {

rs.close();

} catch (SQLException ex) {

}

}

}

}

文件三 封装用户信息类 文件名userInfo.java

package myjava;

public class userInfo {

private String uesrName;

private String userTell;

private String userTellS;

private String userQQ;

private String userEmail;

private String userAdderss;

public userInfo() {

}

public void setUesrName(String uesrName) {

this.uesrName = uesrName;

}

public void setUserTell(String userTell) {

this.userTell = userTell;

}

public void setUserTellS(String userTellS) {

this.userTellS = userTellS;

}

public void setUserQQ(String userQQ) {

this.userQQ = userQQ;

}

public void setUserEmail(String userEmail) {

this.userEmail = userEmail;

}

public void setUserAdderss(String userAdderss) {

this.userAdderss = userAdderss;

}

public String getUesrName() {

return uesrName;

}

public String getUserTell() {

return userTell;

}

public String getUserTellS() {

return userTellS;

}

public String getUserQQ() {

return userQQ;

}

public String getUserEmail() {

return userEmail;

}

public String getUserAdderss() {

return userAdderss;

}

}

文件四 操作控制类文件名 Frame1.java

package myjava;

import java.awt.*;

import javax.swing.*;

import java.awt.Dimension;

import java.awt.Rectangle;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class Frame1 extends JFrame {

JPanel contentPane;

JLabel jLabel1 = new JLabel();

JLabel jLabel2 = new JLabel();

JLabel jLabel3 = new JLabel();

JLabel jLabel4 = new JLabel();

JLabel lblName = new JLabel();

JLabel jLabel6 = new JLabel();

JTextField txtTell = new JTextField();

JTextField txtTellS = new JTextField();

JTextField txtQQ = new JTextField();

JTextField txtEamil = new JTextField();

JTextField txtAddress = new JTextField();

JTextField txtName = new JTextField();

JButton jButton1 = new JButton();

JButton jButton2 = new JButton();

public Frame1() {

try {

setDefaultCloseOperation(EXIT_ON_CLOSE);

jbInit();

} catch (Exception exception) {

exception.printStackTrace();

}

}

/**

* Component initialization.

*

* @throws java.lang.Exception

*/

private void jbInit() throws Exception {

contentPane = (JPanel) getContentPane();

contentPane.setLayout(null);

setSize(new Dimension(400, 488));

setTitle("Frame Title");

jLabel1.setText("电话");

jLabel1.setBounds(new Rectangle(23, 83, 101, 39));

jLabel2.setText("手机");

jLabel2.setBounds(new Rectangle(23, 138, 101, 39));

jLabel3.setText("QQ");

jLabel3.setBounds(new Rectangle(23, 197, 101, 39));

jLabel4.setText("Email");

jLabel4.setBounds(new Rectangle(23, 261, 101, 39));

lblName.setText("姓名");

lblName.setBounds(new Rectangle(23, 33, 101, 39));

jLabel6.setText("地址");

jLabel6.setBounds(new Rectangle(23, 329, 101, 39));

txtTell.setBounds(new Rectangle(120, 89, 260, 33));

txtTellS.setBounds(new Rectangle(120, 146, 260, 33));

txtQQ.setBounds(new Rectangle(120, 199, 260, 33));

txtEamil.setSelectionStart(11);

txtEamil.setBounds(new Rectangle(120, 257, 260, 33));

txtAddress.setBounds(new Rectangle(120, 324, 260, 33));

txtName.setBounds(new Rectangle(120, 41, 260, 33));

jButton1.setBounds(new Rectangle(250, 405, 130, 36));

jButton1.setText("查找");

jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));

jButton2.setBounds(new Rectangle(29, 404, 130, 36));

jButton2.setText("添加");

jButton2.addActionListener(new Frame1_jButton2_actionAdapter(this));

contentPane.add(jLabel1);

contentPane.add(jLabel2);

contentPane.add(jLabel3);

contentPane.add(jLabel4);

contentPane.add(lblName);

contentPane.add(jLabel6);

contentPane.add(txtName);

contentPane.add(txtTell);

contentPane.add(txtTellS);

contentPane.add(txtQQ);

contentPane.add(txtEamil);

contentPane.add(txtAddress);

contentPane.add(jButton2);

contentPane.add(jButton1);

}

//判断输入的内容长度是否合法

private boolean Is(String str,int conut){

boolean is=false;

if(str==null||str.equals("")||str.length()conut){

is=true;

}

return is;

}

//判断是否为字符,不能含有空白字符

private boolean IsLetter(String str,int Count){

boolean is=false;

if(Is(str,Count)){

return is=true;

}

for(int i=0;istr.length();i++){

char ch=str.charAt(i);

if(!Character.isLetter(ch)){

return is=true;

}

}

return is=false;

}

//判断是否为数字,不能含有空白字符

private boolean IsNumber(String str,int conut){

boolean is=false;

if(Is(str,conut)){

return is=true;

}

for(int i=0;istr.length();i++){

char ch=str.charAt(i);

if(!Character.isDigit(ch)ch!='-'){

return is=true;

}

}

return is=false;

}

//判断邮箱是否合法

private boolean IsEmail(String Email,int count){

boolean is=false;

if(Is(Email,count)){

return is=true;

}

int index1=Email.indexOf("@");

int index2=Email.indexOf(".");

if(index1==-1||index2==-1||index1index2){

return is=true;

}

return is;

}

//获得用户信息对象

private userInfo getInfo(){

userInfo u=new userInfo();

String name=txtName.getText().trim();

String Tell=txtTell.getText().trim();

String TellS=txtTellS.getText().trim();

String QQ=txtQQ.getText().trim();

String Email=txtEamil.getText().trim();

String Address=txtAddress.getText().trim();

//判断姓名和地址

if(IsLetter(name,5)||IsLetter(Address,20)){

JOptionPane.showMessageDialog(this,"姓名或地址有误,数字填写只能含有'-'");

return null;

}

//判断邮箱是否合法

if(IsEmail(Email,16)){

JOptionPane.showMessageDialog(this,"邮箱填写有误!");

return null;

}

//判断联系方式中数字部分是否合法

if(IsNumber(Tell,12)||IsNumber(TellS,12)||IsNumber(QQ,12)){

JOptionPane.showMessageDialog(this,"填写联系方法有误,数字填写只能含有'-'");

return null;

}

u.setUesrName(name);

u.setUserTell(Tell);

u.setUserTellS(TellS);

u.setUserQQ(QQ);

u.setUserEmail(Email);

u.setUserAdderss(Address);

return u;

}

private int Insert(userInfo u){

DB D=new DB();

int i=0;

if(u!=null){

String sql="insert into uses values(?,?,?,?,?,?)";

try {

PreparedStatement ps = D.getConnection().prepareStatement(sql);

ps.setString(1,u.getUesrName());

ps.setString(2,u.getUserTell());

ps.setString(3,u.getUserTellS());

ps.setString(4,u.getUserQQ());

ps.setString(5,u.getUserEmail());

ps.setString(6,u.getUserAdderss());

i=ps.executeUpdate();

} catch (SQLException ex) {

ex.printStackTrace();

JOptionPane.showMessageDialog(this,"系统错误,添加失败!"+ex.getMessage());

return i;

}finally{

D.close();

}

}

return i;

}

private void getuserInfo(String userName) {

boolean is=false;

DB D=new DB();

if(IsLetter(userName,5)){

JOptionPane.showMessageDialog(this,"姓名不合法!");

return;

}

String sql="select * from uses where username=?";

try {

PreparedStatement ps = D.getConnection().prepareStatement(sql);

ps.setString(1,userName);

ResultSet rs=ps.executeQuery();

if(!rs.next()){

JOptionPane.showMessageDialog(this,"查询信息不存在!");

}else{

txtName.setText(rs.getString(1));

txtTell.setText(rs.getString(2));

txtTellS.setText(rs.getString(3));

txtQQ.setText(rs.getString(4));

txtEamil.setText(rs.getString(5));

txtAddress.setText(rs.getString(6));

}

} catch (SQLException ex) {

ex.printStackTrace();

JOptionPane.showMessageDialog(this,"系统错误!"+ex.getMessage());

return;

}finally{

D.close();

}

}

//添加按钮监听器

public void jButton2_actionPerformed(ActionEvent e) {

if(Insert(getInfo())==0){

JOptionPane.showMessageDialog(this,"添加信息失败!");

}

}

//查询按钮监听方法

public void jButton1_actionPerformed(ActionEvent e) {

getuserInfo(txtName.getText().trim());

}

}

class Frame1_jButton1_actionAdapter implements ActionListener {

private Frame1 adaptee;

Frame1_jButton1_actionAdapter(Frame1 adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton1_actionPerformed(e);

}

}

class Frame1_jButton2_actionAdapter implements ActionListener {

private Frame1 adaptee;

Frame1_jButton2_actionAdapter(Frame1 adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.jButton2_actionPerformed(e);

}

}


分享题目:添加信息java代码 java添加用户信息
本文网址:http://ybzwz.com/article/dooosci.html