java怎样将读取数据写入数据库

java怎样将读取数据写入数据库~

就要链接数据库,可以通过JDBC链接。
首先,在连接数据库之前必须保证SQL Server 2012是采用SQL Server身份验证方式而不是windows身份验证方式,开始如下配置:
一、因为SQL Server 2012装好后,默认协议是没有开启的,所以要打开SQL Server配置管理器中开启。
1、安装好SQL Server 2012后,运行 开始 → 所有程序 → Microsoft SQL Server 2012 → 配置工具 →SQL Server配置管理器
2、在左边栏找到 SQL Server网络配置选项,点开它的小箭头,会看到“【你的数据库名】的协议” (图中是ERIC2012的协议),选中它,看右边栏。
(1)如果Named Pipes 未启用,则右键→启用
(2)右键单击 TCP/IP,选择 启用
(3)双击TCP/IP(右键→属性),在弹出的窗口中选择 “IP地址” 选项卡,将IP1和IP10的【IP地址】设为127.0.0.1,并将所有【IPx】的【已启用】设为是。接着,拖动下拉条到最下方,将 IPAll 中的【TCP端口】设成 【1433】,其余不变。
3、重新启动计算机。
4、接下来使用telnet命令测试1433端口是否打开。首先要保证telnet服务开启。
5、完成上一步后。开始菜单 → 运行cmd → 输入:telnet 127.0.0.1 1433,(注意telnet与127之间有空格,1与1433之间有空格)。
6、若提示“不能打开到主机的连接,在端口 1433: 连接失败”,则说明1433端口没有打开,需要重新进行以上配置。

将数据查询出来放在list中,然后写入文件。
给你个写入的类,查询数据自己如果能搞定最好了
FileWriter fileWriter=new FileWriter("c:\\Result.txt");
int [] a=new int[]{11112,222,333,444,555,666};
for (int i = 0; i < a.length; i++) {
fileWriter.write(String.valueOf(a[i])+" ");
}
fileWriter.flush();
fileWriter.close();
上面例子中的a也可以是list

就要链接数据库,可以通过JDBC链接。
首先,在连接数据库之前必须保证SQL Server 2012是采用SQL Server身份验证方式而不是windows身份验证方式,开始如下配置:
一、因为SQL Server 2012装好后,默认协议是没有开启的,所以要打开SQL Server配置管理器中开启。
1、安装好SQL Server 2012后,运行 开始 → 所有程序 → Microsoft SQL Server 2012 → 配置工具 →SQL Server配置管理器
2、在左边栏找到 SQL Server网络配置选项,点开它的小箭头,会看到“【你的数据库名】的协议” (图中是ERIC2012的协议),选中它,看右边栏。
(1)如果Named Pipes 未启用,则右键→启用
(2)右键单击 TCP/IP,选择 启用
(3)双击TCP/IP(右键→属性),在弹出的窗口中选择 “IP地址” 选项卡,将IP1和IP10的【IP地址】设为127.0.0.1,并将所有【IPx】的【已启用】设为是。接着,拖动下拉条到最下方,将 IPAll 中的【TCP端口】设成 【1433】,其余不变。
3、重新启动计算机。
4、接下来使用telnet命令测试1433端口是否打开。首先要保证telnet服务开启。
5、完成上一步后。开始菜单 → 运行cmd → 输入:telnet 127.0.0.1 1433,(注意telnet与127之间有空格,1与1433之间有空格)。
6、若提示“不能打开到主机的连接,在端口 1433: 连接失败”,则说明1433端口没有打开,需要重新进行以上配置。

1:导入mysql的驱动jar包,mysql-connector-java-5.1.8-bin.jar
2:写代码连接数据库,如下:
package 数据库_向数据库插入数据;
import java.sql.Connection;
import java.sql.DriverManager;
public class DatabaseConnection {
private static Connection conn = null;
public static Connection getCon() {
try {
Class.forName("com.mysql.jdbc.Driver"); //加载数据库连接驱动
String user = "root";
String psw = "XXX"; //XXX为自己的数据库的密码
String url = "jdbc:mysql://localhost:3306/ZZZ"; //ZZZ为连接的名字
conn = DriverManager.getConnection(url, user, psw); //获取连接
} catch (Exception e) {
System.out.println("连接数据库失败");
e.printStackTrace();
}
return conn;
}
}
三:封装读取的数据
package 数据库_向数据库插入数据;
//尽量将属性定义为私有的,写出对应的setXXX和getXXX的方法
public class Employee {
private int empId;
private String empName;
private int empAge;
private String empSex;

public Employee(){}

public int getEmpId() {
return this.empId;
}
public void setEmpId(int id) {
this.empId = id;
}

public String getEmpName() {
return this.empName;
}
public void setEmpName(String name) {
this.empName = name;
}

public int getEmpAge() {
return this.empAge;
}
public void setEmpAge(int age) {
this.empAge = age;
}

public String getEmpSex() {
return this.empSex;
}
public void setEmpSex(String sex) {
this.empSex = sex;
}
}
四。向数据库插入数据(包括增删改查)
package 数据库_向数据库插入数据;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
//EmployeeOperation类用于操作数据库,以下的编写方法是JAVA软件编程的一种设计模式,称为!!!!! 单例模式,!!!!!!!
//方法中先判断该类的对象是否为空,只有为空才创建(new) ,因此保证该对象在程序中永远是唯一的,可以避免重复创建对象造成的系统内存被过多占用
public class EmployeeOperation {
private static EmployeeOperation instance = null;

public static EmployeeOperation getInstance() { //返回EmployeeOperation类实例的静态方法,单例模式!!!!
if (instance == null) {
instance = new EmployeeOperation();
}
return instance;
}

public boolean saveEmployee(Employee emp) { //向数据库中加入数据
boolean result = false;
Connection conn = null;
try {

conn = DatabaseConnection.getCon(); //建立数据库连接
String sqlInset = "insert into company.tb_employee(empId, empName, empAge, empSex) values(?, ?, ?, ?)";
PreparedStatement stmt = conn.prepareStatement(sqlInset); //会抛出异常

stmt.setInt(1, emp.getEmpId()); //设置SQL语句第一个“?”的值
stmt.setString(2, emp.getEmpName()); //设置SQL语句第二个“?”的值
stmt.setInt(3, emp.getEmpAge()); //设置SQL语句第三个“?”的值
stmt.setString(4, emp.getEmpSex()); //设置SQL语句第四个“?”的值
int i = stmt.executeUpdate(); //执行插入数据操作,返回影响的行数
if (i == 1) {
result = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally { //finally的用处是不管程序是否出现异常,都要执行finally语句,所以在此处关闭连接
try {
conn.close(); //打开一个Connection连接后,最后一定要调用它的close()方法关闭连接,以释放系统资源及数据库资源
} catch(SQLException e) {
e.printStackTrace();
}
}

return result;

}

public List<Employee> selectEmployee() { //从数据库中查询所需数据
List<Employee> empList = new ArrayList<Employee>();
Connection conn = null;
try {
conn = DatabaseConnection.getCon();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from company.tb_employee");//执行SQL并返回结果集
while (rs.next()) {
Employee emp = new Employee();
emp.setEmpId(rs.getInt("empId")); //从结果集rs中获取内容时,若为字符串类型的,用rs.getString("string")方法
emp.setEmpName(rs.getString("empName")); //其中str为想要从 数据库的 表 中获取的信息
emp.setEmpAge(rs.getInt("empAge")); //若为int类型,用rs.getInt(number);
emp.setEmpSex(rs.getString("empSex"));
empList.add(emp);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close(); //关闭连接
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return empList; //返回结果
}

public boolean updateEmployee(Employee emp) { //根据员工的编号更改员工的年龄信息
boolean result = false;
Connection conn = null;
try {
conn = DatabaseConnection.getCon();
String sql = "update company.tb_employee set empAge=? where empId=?"; //update语句
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, emp.getEmpAge()); //设置SQL语句第一个"?"的参数值
stmt.setInt(2, emp.getEmpId()); //设置SQL语句第二个"?"的参数值
int flag = stmt.executeUpdate(); //执行修改操作,返回影响的行数
if (flag == 1) { //修改成功返回true
result = true;
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}

public boolean deleteEmployeeById(Employee emp) {
boolean result = false;
Connection conn = null;
try {
conn = DatabaseConnection.getCon();
String sql = "delete from company.tb_employee where empId = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setInt(1, emp.getEmpId());
int i = stmt.executeUpdate();
if (i == 1) {
result = true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
}

连接数据库 用SQL语句写呗

Java读取TXT文档中的数据并赋值给动态数组
答:源代码如下,自己运行下:import java.io.BufferedReader;import java.io.FileReader;import java.util.ArrayList;import java.util.List;import javax.swing.JFrame;public class Test extends JFrame { static List<Double> x = new ArrayList<Double>();static List<Double> y = new ArrayList<Double...

java怎样将读取数据写入数据库
答:import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit...

怎么用java将一个excel里面数据读出并写入另一个excel?
答:一、在开始进行Java读写Excel前,需要先下一个jxl的jar包,这个jar包中提供了相关读写Excel的方法,将jxl.jar放到classpath下或者在工程的buildpath中添加jxl.jar后,便可以开始Java读写Excel了。二、Java读取Excel数据,首先,创建一个xls文件(如:jxltest.xls),然后在文件中添加一些数据,Excel文件创...

从excel表格读取数据用Java代码实现批量上传写入数据库
答://写入数据并关闭文件 book.write();book.close();}catch(Exception e){ System.out.println(e);} } } 编译执行后,会在当前位置产生一个Excel文件。三、读取文件 以刚才我们创建的Excel文件为例,做一个简单的读取操作,程序代码如下://读取Excel的类 import java.io.*;import jxl.*;public ...

JAVA中怎样把用户输入的字符串存入数组中?
答:import java.util.Scanner;import java.util.InputMismatchException;public class saveInputToArr { public static void main(String[] args) { Scanner scan = null;try { scan = new Scanner(System.in);System.out.print( "请输入个数: " );int inputNum = scan.nextInt();if( inputNum <...

java能否读取csv文件的同时也写入数据?
答:回答:首先,答案是肯定的。如果是指写入数据库,则 看复杂度,如果简单的,按TEXT读、拆分一下就可以。 如果复杂,可以当EXCEL,使用POI读进去。 读取足够的信息,就使用JDBC、等,写入数据库 如果是写csv文件本身,那使用RandomAccess,读写指定的位置

怎样用Java实现从文本文档中读取数据并存入数据库
答:import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;/** * 文件读取和写入数据库 * @author 樊云升 * */public class FilesReader {public FilesReader(){}/** * 读取文件内容 * @param FILE * @return */public ...

java怎样将读取数据写入数据库
答:1、安装好SQL Server 2012后,运行 开始 → 所有程序 → Microsoft SQL Server 2012 → 配置工具 →SQL Server配置管理器 2、在左边栏找到 SQL Server网络配置选项,点开它的小箭头,会看到“【你的数据库名】的协议” (图中是ERIC2012的协议),选中它,看右边栏。(1)如果Named Pipes 未启用...

如何使用Java将把读取的txt文件内容写进MySQL
答:String str="将txt文件内容写到一个字符串中";然后用 insert str into table 这样的insert语句插入到数据库中,当然前提条件是数据库中要存在这样的一个数据库表。对txt文件进行分割,逐个读进数据库,可能比较耗时!

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网