如何把java对象存入数据库

如何把java对象存入数据库,第1张

假设有这么个对象

import java.io.Serializable

public class MyObject implements Serializable {

private static final long serialVersionUID = 1L

private int i

public int getI() {

return i

}

public void setI(int i) {

this.i = i

}

}

//测试 的方法如下

import java.io.ByteArrayInputStream

import java.io.ByteArrayOutputStream

import java.io.IOException

import java.io.ObjectInputStream

import java.io.ObjectOutputStream

import java.sql.Connection

import java.sql.DriverManager

import java.sql.PreparedStatement

import java.sql.ResultSet

public class test {

public static void main(String[] args) throws IOException,

ClassNotFoundException {

MyObject obj = new MyObject()

obj.setI(4567)

write(Object2Bytes(obj))

// read()

}

public static void write(byte[] b) throws ClassNotFoundException {

System.out.println(b.length)

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")

try {

Connection con = DriverManager.getConnection("jdbc:odbc:temp")

String sql = "insert into tab values(?)"

PreparedStatement pstmt = con.prepareStatement(sql)

pstmt.setBytes(1, b)

pstmt.execute()

pstmt.close()

con.close()

} catch (Exception e) {

e.printStackTrace()

}

}

public static void read() throws ClassNotFoundException {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")

try {

Connection con = DriverManager.getConnection("jdbc:odbc:temp")

String sql = "select * from tab"

PreparedStatement pstmt = con.prepareStatement(sql)

ResultSet res = pstmt.executeQuery()

while (res != null &&res.next()) {

byte[] b = res.getBytes("key")

System.out.println(b.length)

MyObject obj = (MyObject) Bytes2Object(b)

System.out.println(obj.getI())

}

pstmt.close()

con.close()

} catch (Exception e) {

e.printStackTrace()

}

}

// 将对象转换成字节数组

public static byte[] Object2Bytes(Object obj) throws IOException {

ByteArrayOutputStream baos = new ByteArrayOutputStream()

ObjectOutputStream oos = new ObjectOutputStream(baos)

oos.writeObject(obj)

return baos.toByteArray()

}

// 将字节数组转换成为对象

public static Object Bytes2Object(byte[] b) throws IOException,

ClassNotFoundException {

ByteArrayInputStream bais = new ByteArrayInputStream(b)

ObjectInputStream ois = new ObjectInputStream(bais)

Object obj = ois.readObject()

return obj

}

}

简单啊,建立一个包。

cn.temp.object存放临时类。

使用File类来写文件,保存为 [ 类.java ]

再动态编译。

OK

只能写个大概的,要写数据到数据库中,先得在数据库中建库,库里建表,表里建字段,然后java里建立数据库连接,用SQL语言写数据到表中的字段

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

//String url="jdbc:microsoft:sqlserver://localhost:1433DatabaseName=数据库名"//7.0、2000

String url="jdbc:sqlserver://localhost:1433DatabaseName=数据库名"//2005

Connection conn=null

conn= DriverManager.getConnection(url,用户名,密码)

PreparedStatement pst=null

pst=conn.prepareStatement("Insert Into grade(表名) Values (?)")

pst.setInt(1,你要写的整弄数据)

//pst.setString(2,你要写的字符串数据)

pst.addBatch()

pst.executeBatch()


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存