java怎么将数据存入数据库中并用界面显示

java怎么将数据存入数据库中并用界面显示,第1张

public class frame1 extends JFrame {

public frame1() {

setContentPane(getPanel());

setSize(200, 200);

setDefaultCloseOperation(EXIT_ON_CLOSE);

setVisible(true);

}

private JPanel getPanel() {

JPanel panel = new JPanel(new BorderLayout());

JTextArea text = new JTextArea();

//将信息整合成字符串赋值给JTextArea,

textsetText("姓名:Duck\n密码:1234 。。。。\n");

textsetVisible(true);

paneladd(text, BorderLayoutCENTER);

return panel;

}

public static void main(String[] args) {

new frame1();

}

}

我的也没错啊,我是将你查询到的数据库的内容整合成一个字符串,在d出界面上的JTextArea 这个控件上显示,你可以把我给的例子运行一下看看

我无语了,JFrame不是一个窗体么?

好吧,换一种说法,你想怎样显示这些数据?你所谓的窗体时另外d出来的还是在原有窗体上

Java添加UI界面不一定需要连接数据库。连接数据库是为了实现对数据库的增删改查等 *** 作,如果你的UI界面不需要对数据库进行 *** 作,那么就不需要连接数据库。如果需要对数据库进行 *** 作,那么就需要连接数据库。在Java中,可以使用JDBC来连接数据库。

String sql="select from datas where 1=1 ";

if(!conditionequals("")){ ///如果某条件的值不为空,就加上下面的语句

sql=sql+" and xx="+ condition;

}

////其他8个也一样,最后就得到了一个sql语句

if(!conditionequals("")){ ///如果某条件的值不为空,就加上下面的语句

sql=sql+" and xx="+ condition;

}

if(!conditionequals("")){ ///如果某条件的值不为空,就加上下面的语句

sql=sql+" and xx="+ condition;

}

分三个包,自己建个包,导进去就ok了,数据库是access的。

package 登录;

import javaawtEventQueue;

public class Cilent {

private JFrame frame;

private JTextField textField;

private JPasswordField passwordField;

/

Launch the application

/

public static void main(String[] args) {

EventQueueinvokeLater(new Runnable() {

public void run() {

try {

Cilent window = new Cilent();

windowframesetVisible(true);

} catch (Exception e) {

eprintStackTrace();

}

}

});

}

/

Create the application

/

public Cilent() {

initialize();

}

/

Initialize the contents of the frame

/

private void initialize() {

frame = new JFrame();

framesetTitle("登陆界面");

framesetBounds(100, 100, 450, 300);

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

framegetContentPane()setLayout(null);

framesetResizable(false);

JLabel lblNewLabel = new JLabel("用户名");

lblNewLabelsetBounds(38, 43, 80, 34);

framegetContentPane()add(lblNewLabel);

textField = new JTextField();

textFieldsetBounds(155, 42, 227, 37);

framegetContentPane()add(textField);

textFieldsetColumns(10);

JLabel label = new JLabel("密 码");

labelsetBounds(38, 115, 80, 34);

framegetContentPane()add(label);

passwordField = new JPasswordField();

passwordFieldsetBounds(155, 115, 227, 37);

framegetContentPane()add(passwordField);

JButton btnNewButton = new JButton("登 录");

btnNewButtonsetBounds(60, 187, 115, 34);

framegetContentPane()add(btnNewButton);

btnNewButtonaddActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

// TODO Auto-generated method stub

UserCheck UC=new UserCheck(textFieldgetText(),StringvalueOf(passwordFieldgetPassword()));

if(UCgetI()!=0) //有此用户

{

framesetVisible(false);

}

else

{

textFieldsetText("");

passwordFieldsetText("");

}

}

});

JButton button = new JButton("取 消");

buttonsetBounds(242, 187, 115, 34);

framegetContentPane()add(button);

buttonaddActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent arg0) {

// TODO Auto-generated method stub

textFieldsetText("");

passwordFieldsetText("");

}

});

}

}

//

package 登录;

import javasqlConnection;

import javasqlDriverManager;

import javasqlPreparedStatement;

import javasqlResultSet;

import javasqlSQLException;

import javasqlStatement;

import *** 作处理UsersCL;

/@author 20111024

检测登录的用户在数据库中有无,若没有,则提示没有此用户,

若有,则判断级别:普通用户还是管理员。

/

public class UserCheck {

private int i=0; //用户级别:0不是用户、1是管理员、2是普通用户

UserCheck(String name ,String password)

{

String jdriver="sunjdbcodbcJdbcOdbcDriver";

String connectDB="jdbc:odbc:Students";

Statement stmt=null;

ResultSet rs=null;

Connection con=null;

try {

ClassforName(jdriver);

con=DriverManagergetConnection(connectDB);

stmt=concreateStatement();

String query="select from users where name='"+name+"' and passwd='"+password+"'";

rs=stmtexecuteQuery(query);

if(rsnext())

{

//数据库中有此用户,访问成功

i=IntegerparseInt(rsgetString(3));

UsersCL UL=new UsersCL(i);

}

else

{

i=0; //没有用户是默认是0级

}

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

eprintStackTrace();

} catch (SQLException e) {

// TODO Auto-generated catch block

eprintStackTrace();

}

}

public int getI() {

return i;

}

}

//

package *** 作处理;

import javaawtEventQueue;

public class UsersCL implements ActionListener{

private JFrame frame;

private JTextField textField;

private JTextField textField_1;

private JTextField textField_2;

private JTextField textField_3;

private int i=0;

private JLabel label_3;

private JTextField textField_4;

public UsersCL(int i) {

thisi=i;

frame = new JFrame();

framesetTitle("用户处理界面");

framesetBounds(100, 100, 450, 300);

framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

framegetContentPane()setLayout(null);

framesetResizable(false);

framesetVisible(true);

JLabel lblNewLabel = new JLabel("学 号");

lblNewLabelsetBounds(24, 32, 74, 29);

framegetContentPane()add(lblNewLabel);

JLabel label = new JLabel("姓 名");

labelsetBounds(24, 71, 74, 29);

framegetContentPane()add(label);

JLabel label_1 = new JLabel("年 龄");

label_1setBounds(24, 110, 74, 29);

framegetContentPane()add(label_1);

label_3 = new JLabel("性 别");

label_3setBounds(24, 149, 74, 29);

framegetContentPane()add(label_3);

JLabel label_2 = new JLabel("状 态");

label_2setBounds(24, 195, 74, 29);

framegetContentPane()add(label_2);

textField = new JTextField();

textFieldsetBounds(101, 34, 113, 25);

framegetContentPane()add(textField);

textFieldsetColumns(10);

textField_1 = new JTextField();

textField_1setColumns(10);

textField_1setBounds(101, 73, 113, 25);

framegetContentPane()add(textField_1);

textField_2 = new JTextField();

textField_2setColumns(10);

textField_2setBounds(101, 112, 113, 25);

framegetContentPane()add(textField_2);

textField_3 = new JTextField();

textField_3setEditable(false);

textField_3setColumns(10);

textField_3setBounds(101, 199, 288, 25);

framegetContentPane()add(textField_3);

textField_4 = new JTextField();

textField_4setColumns(10);

textField_4setBounds(101, 149, 113, 25);

framegetContentPane()add(textField_4);

if(1==i)

{

JButton btnNewButton = new JButton("追 加");

btnNewButtonsetBounds(276, 41, 113, 29);

framegetContentPane()add(btnNewButton);

btnNewButtonaddActionListener(this);

btnNewButtonsetActionCommand("追加");

JButton button_1 = new JButton("删 除");

button_1setBounds(276, 145, 113, 29);

framegetContentPane()add(button_1);

button_1addActionListener(this);

button_1setActionCommand("删除");

}

JButton button = new JButton("查 询");

buttonsetBounds(276, 91, 113, 29);

framegetContentPane()add(button);

buttonaddActionListener(this);

buttonsetActionCommand("查询");

}

@Override

public void actionPerformed(ActionEvent e) {

// TODO Auto-generated method stub

String name,age,sex,query=null;

int num,age1,count=0;

num=IntegerparseInt(textFieldgetText());

name=textField_1getText();

age1=IntegerparseInt(textField_2getText());

sex=textField_4getText();

if(egetActionCommand()equals("追加"))

{

query="insert into students values("+num+","+"'"+name+"',"+age1+",'"+sex+"');";

count=1;

}

else if(egetActionCommand()equals("查询"))

{

query="select from students where XSB="+num+";";

count=2;

}

else if(egetActionCommand()equals("删除"))

{

query="delete from students where XSB="+num+" and name="+"'"+name+"'";

count=3;

}

Statement stmt=null;

ResultSet rs=null;

Connection con=null;

String jdriver="sunjdbcodbcJdbcOdbcDriver";

String connectDB="jdbc:odbc:Students";

String query1=null;

try {

ClassforName(jdriver);

con=DriverManagergetConnection(connectDB);

stmt=concreateStatement();

if(count==1)

{

query1="select from students where XSB="+num+";";

rs=stmtexecuteQuery(query1);

if(rsnext())

textField_3setText("已经由此记录,不能追加!");

else

{

stmtexecuteUpdate(query);

textField_3setText("已经追加完成!");

}

}

else if(2==count)

{

stmtexecuteQuery(query);

rs=stmtexecuteQuery(query);

if(rsnext())

{

textField_3setText("已查找到此记录!");

}

else

{

textField_3setText("没有此记录,可以追加!");

}

}

else if(3==count)

{

query1="select from students where XSB="+num+" and name="+"'"+name+"'";

rs=stmtexecuteQuery(query1);

if(rsnext())

{

stmtexecuteUpdate(query);

textField_3setText("已删除此记录!");

}

else

textField_3setText("无此记录!");

}

} catch (ClassNotFoundException e1) {

// TODO Auto-generated catch block

e1printStackTrace();

} catch (SQLException e1) {

// TODO Auto-generated catch block

e1printStackTrace();

}

finally{

//关闭资源

if(stmt!=null){

try {

stmtclose();

} catch (Exception e2) {

// TODO: handle exception

}

stmt=null;

}

if(con!=null){

try {

conclose();

} catch (Exception e2) {

// TODO: handle exception

}

con=null;

}

}

}

}

写一个javaBean,比如叫Result, 这里只给你个示范, private int id; private String name; public void setId(int id){ thisid = id; } public int getId(){ return id; } public void setName(String name){ thisname = name; } public String getName(){ return name; } 然后得到结果后把结果设成javaBean,再把javaBean装到list里 LIst resultList = new ArrayLIst();

while(rsnext()){ Result result = new Result();

resultsetId(rsgetInt("memID"));

resultsetName(rsgetString("memNam"));

resultListadd(result);} 最后再你需要的地方展现就可以了 for(int i=0; i<resultListsize(); i++){ Result result = listget(i);JLable lable1 = new JLable(resultgetId); JLable lable2 = new JLable(resultgetName); }只是写了思路,不是在工具上写的,可能有语法错误,还不懂在m我。

我帮你写了一个最简单的、满足了你的要求的项目。登录界面中,输入仓库名字,然后连接数据,查询是否存在改仓库名,若存在,则向主窗体跳转,若不存在,则提示仓库名不存在。

代码如下:

import javaawtBorderLayout;

import javaxswingJPanel;

import javaxswingJFrame;

import javaxswingJLabel;

import javaawtRectangle;

import javasqlConnection;

import javasqlDriverManager;

import javasqlResultSet;

import javasqlStatement;

import javaxswingJTextField;

import javaxswingJButton;

public class MySJK extends JFrame {

private static final long serialVersionUID = 1L;

private JPanel jContentPane = null;

private JLabel jLabel = null;

private JTextField jTextField = null;

private JButton jButton = null;

/

This is the default constructor

/

public MySJK() {

super();

initialize();

}

/

This method initializes this

@return void

/

private void initialize() {

thissetSize(300, 200);

thissetContentPane(getJContentPane());

thissetTitle("登录");

thissetVisible(true);

}

/

This method initializes jContentPane

@return javaxswingJPanel

/

private JPanel getJContentPane() {

if (jContentPane == null) {

jLabel = new JLabel();

jLabelsetBounds(new Rectangle(30, 16, 130, 18));

jLabelsetText("请输入要登录的仓库:");

jContentPane = new JPanel();

jContentPanesetLayout(null);

jContentPaneadd(jLabel, null);

jContentPaneadd(getJTextField(), null);

jContentPaneadd(getJButton(), null);

}

return jContentPane;

}

/

This method initializes jTextField

@return javaxswingJTextField

/

private JTextField getJTextField() {

if (jTextField == null) {

jTextField = new JTextField();

jTextFieldsetBounds(new Rectangle(30, 45, 194, 27));

}

return jTextField;

}

/

This method initializes jButton

@return javaxswingJButton

/

private JButton getJButton() {

if (jButton == null) {

jButton = new JButton();

jButtonsetBounds(new Rectangle(149, 87, 74, 26));

jButtonsetText("登录");

jButtonaddActionListener(new javaawteventActionListener() {

public void actionPerformed(javaawteventActionEvent e) {

String myckstr=jTextFieldgetText();

String checkstr="false";

Connection con=null;

try{

ClassforName("sunjdbcodbcJdbcOdbcDriver");

con=DriverManagergetConnection("jdbc:odbc:CANGKU","","");

Statement sta=concreateStatement();

ResultSet rs=staexecuteQuery("select from 仓库 where ckid='"+myckstr+"'");

if(rsnext()){

checkstr="true";

}

rsclose();

staclose();

}

catch(Exception ee){

eeprintStackTrace();

}

finally{

try{

conclose();

}

catch(Exception ee){

eeprintStackTrace();

}

}

if(checkstrequals("true")){

////登录系统的主窗体

}

else{

/////提示仓库名不存在

}

}

});

}

return jButton;

}

public static void main(String args[]){

new MySJK();

}

}

////记得给我分哦!^_^

1写一个 *** 作数据库的通用DAO(假设为Daojava),提供·加载数据库驱动和获取数据库连接的方法:voidgetConnection();·执行查询的方法:ResultSetexecuteQuery(Stringsql);·执行更新的方法:IntegerexecuteUpdate(Stringsql);·关闭资源的方法:voidreleaseResource(Connectioncon);2在你的界面中把用户名和密码取出来·username=yourTextFieldgetText();·password=yourPasswordFieldgetText();3匹配·验证username、password是否是有效输入值(例如:不能包含特殊字符,不能有注入嫌疑等)·Stringsql="selectfromyourTablewhereusername='"+username+"'andpassword='"+password+"'";·DaoexecuteQuery(sql):如果有结果说明合法,否则不合法。不懂Connect我

以上就是关于java怎么将数据存入数据库中并用界面显示全部的内容,包括:java怎么将数据存入数据库中并用界面显示、java添加ui界面需要连接数据库吗、java GUI *** 作sql server数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/10134738.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-05
下一篇 2023-05-05

发表评论

登录后才能评论

评论列表(0条)

保存