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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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端口没有打开,需要重新进行以上配置。
参考技术A 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;

参考技术B 连接数据库 用SQL语句写呗

以上是关于java怎样将读取数据写入数据库的主要内容,如果未能解决你的问题,请参考以下文章

从excel表格读取数据用Java代码实现批量上传写入数据库

怎样通过JSP页面上传个Excel文件,并实现Java读取EXCEL存入数据库

怎样从数据库读取图片

计算机内存读取写入原理

java读取csv写入数据库

在Java中直接将数据写入RAM,然后再读取? [关闭]