「java保存语句」java存储数据的方法

博主:adminadmin 2022-11-26 03:33:05 103

今天给各位分享java保存语句的知识,其中也会对java存储数据的方法进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java 将数据保存到数据库的问题 SQL语句

感觉问题不是很明确。所谓的表单是指一个表单只包含一组name

id

sex信息吗?还是一个象excel一样的表格,或csv格式的,里面存了n多人的name

id

sex信息,每组是一行。还有表单里的信息难道和数据库里的信息没有关联的吗,纯粹是数据库里第n条对应第n张表单或是表格中第n条记录。不过不管怎么样的情况,这里只能是一条一条从表单中读出然后一条一条插入。不过可以用数据库的“批量更新”方法及PreparedStatement,这可以稍微提高点效率,比如,

String

s

=

"insert

into

T2(B,

C,

name,

id,

sex)

values(?,

?,

?,

?,

?)"

PreparedStatement

pst

=

conn.prepareStatement(s);

rs_T

=

executeQuery("select

B,

C

from

T");

while(rs_T.next()){

pst.setString(1,

rs_T.getString("B"));

pst.setString(2,

rs_T.getString("C"));

pst.setString(3,

name);

//这里的name,id和sex在表单中取得

pst.setString(4,

id);

pst.setString(5,

sex);

pst.addBatch();

}

pst.executeBatch();

不过这有可能不是楼主想要的。

java 中如何保存String的值?

首先 你要把这些信息保存到哪里?如果只是保存到一个txt文件中,非常简单

import java.io.*;

import java.util.*;

import com.ufoo.portal.utils.IDMaker;

public class Information

{

private Properties setting;

private File file;

private String userName ="admin"; //默认属性 用户名和密码

private String passwd="123456";

public Information()

{

setting = new Properties();

try

{

file = new File("D:\\sims_log\\","log.txt"); //创建文件log.txt 自己在D盘把sims_log 文件夹建好

if(!file.exists()) //若文件不存在

{

File parentDir=new File(file.getParent());

if(!parentDir.exists())

{

parentDir.mkdirs();

}

file.createNewFile(); //新建

}

setting.load(new FileInputStream(file)); //载入文件

}

catch(Exception e)

{

}

setInformation(userName,passwd); //调用存储属性方法

}

public void setInformation(String UserName,String passwd)

{

try{

setting.setProperty("user name", UserName); //存储用户名 每个属性对应一个键 例如用户名的键就是 user name

setting.setProperty("password", passwd); //存储密码

setting.store(new FileOutputStream(file), ""); //保存到文件 "log.txt"

}

catch(Exception e)

{

e.printStackTrace();

}

}

public String getInformation(String UserNameKey)

{

return setting.getProperty(UserNameKey); //获得用户名

}

public static void main(String args[])

{

Information info=new Information();

info.setInformation("123123","12345"); //设置用户名和密码

System.out.println("the user name is:"+info.getInformation("user name"));//这里user name是键名,打印出用户名

info.setInformation("lisi","12345"); //设置用户名和密码

System.out.println("the user name is:"+info.getInformation("user name"));//这里user name是键名,打印出用户名

}

}

java如何保存文件

这是我原来做的例子,里面有文件储存的内容,代码不多,给你参考参考.

/**

* 五个按钮的故事,西西哈。

*/

import java.awt.*;

import java.awt.event.*;

import java.io.*;

public class FileMessage extends Frame implements ActionListener

{

private static final long serialVersionUID = 10L;

Dialog dia;

private Panel p;

private File fi;

Process po=null;

private String s;

private TextArea ta;

private FileDialog fd;

private Button b1,b2,b3,b4,b5;

private Button b6;

public FileMessage()

{

super("文本文件处理");

setBackground( Color.LIGHT_GRAY );

setLocation(200,300);

setResizable( false);

setVisible( true);

addWindowListener( new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

System.exit( 0);

}

});

}

public void init()

{

ta=new TextArea("\n\n\n\n\n\t\t\t\t文本显示区");

ta.setSize(30,5);

ta.setEditable(false);

add( ta,"North");

p=new Panel();

add( p,"Center");

b1=new Button("浏览");

b2=new Button("保存");

b3=new Button("清空");

b4=new Button("关闭");

b5=new Button("独立打开");

b6=new Button("确定");

p.add(b1);

p.add(b2);

p.add(b3);

p.add(b4);

p.add(b5);

b1.addActionListener(this);

b2.addActionListener(this);

b3.addActionListener(this);

b4.addActionListener(this);

b5.addActionListener(this);

b6.addActionListener(this);

fd=new FileDialog(this,"请选择文件",FileDialog.LOAD);

fd.setDirectory("f:\\note");

pack();

dia=new Dialog(this,"注意",true);

dia.setLayout(new BorderLayout());

Panel p1=new Panel();

p1.add( b6);

dia.add(new Label(" 请先选择文件"),BorderLayout.CENTER);

dia.add( p1,BorderLayout.SOUTH);

dia.addWindowListener( new WindowAdapter()

{

public void windowClosing(WindowEvent e)

{

dia.setVisible( false);

}

});

dia.setLocation(310,370);

dia.setSize(200,130);

}

public static void main(String[] args)

{

new FileMessage().init();

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==b1)

{

fd.setVisible(true);

s=fd.getDirectory()+fd.getFile();

fi=new File(s);

byte[] b=new byte[(int)fi.length()];

try

{

new FileInputStream(fi).read(b);

ta.setText(new String(b,0,(int)fi.length()));

}

catch(Exception e1){}

ta.setEditable(true);

}

else if(e.getSource()==b2)

{

try

{

if(ta.getText().equals("保存成功")||ta.getText() .equals( ""))

{}

else

{

new FileOutputStream(fi).write(ta.getText().getBytes());

ta.setText("保存成功");

ta.setEditable(false);

}

}

catch(FileNotFoundException e1)

{

ta.setText(e1.getMessage());

}

catch(IOException e1)

{

ta.setText("出现IOException异常");

}

}

else if(e.getSource()==b4)

System.exit(0);

else if(e.getSource()==b3)

{

ta.setText("");

ta.setEditable( false);

}

else if(e.getSource()==b5)

{

if(s==null)

{

dia.setVisible(true);

}

else

{

try

{

po=Runtime.getRuntime().exec("notepad.exe "+s);

}

catch(Exception ei)

{}

}

}

else if(e.getSource() ==b6)

{

dia.setVisible(false);

}

}

}

关于java保存语句和java存储数据的方法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-26,除非注明,否则均为首码项目网原创文章,转载请注明出处。