用java怎样把数据存到数据库中?

用java怎样把数据存到数据库中?,第1张

只能写个大概的,要写数据数据库中,先得在数据库中建库,库里建表,表里建字段,然后java里建立数据库连接,用SQL语言写数据到表中的字段\x0d\x0aClass.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance()\x0d\x0a//String url="jdbc:microsoft:sqlserver://localhost:1433DatabaseName=数据库名"//7.0、2000\x0d\x0aString url="jdbc:sqlserver://localhost:1433DatabaseName=数据库名"//2005\x0d\x0aConnection conn=null\x0d\x0aconn= DriverManager.getConnection(url,用户名,密码)\x0d\x0aPreparedStatement pst=null\x0d\x0apst=conn.prepareStatement("Insert Into grade(表名) Values (?)")\x0d\x0apst.setInt(1,你要写的整弄数据)\x0d\x0a//pst.setString(2,你要写的字符串数据)\x0d\x0apst.addBatch()\x0d\x0apst.executeBatch()

用java连接数据库啊。下面给点例子可以参考,不大好,凑合看

import java.sql.Connection

import java.sql.DriverManager

import java.sql.PreparedStatement

import java.sql.ResultSet

import java.sql.SQLException

public class JdbcConnection

{

private String driver

private String dbname

private String username

private String password

private String url

private Connection con

private PreparedStatement pstmt

private ResultSet rs

public JdbcConnection(){

this.driver="com.microsoft.sqlserver.jdbc.SQLServerDriver"

this.dbname=数据库名"

this.username="数据库账号"

this.password="数据库密码"

this.url="地址加数据库名"

}

//创建连接方法

public Connection getconnection()

{

try

{

Class.forName(driver)

con=DriverManager.getConnection(url,username,password)

} catch (ClassNotFoundException e)

{

// TODO Auto-generated catch block

e.printStackTrace()

} catch (SQLException e)

{

// TODO Auto-generated catch block

e.printStackTrace()

}

return con

}

//关闭连接方法

public void coloseConnection(Connection con,ResultSet rs,PreparedStatement pstmt)

{

if(rs!=null)

{

try

{

rs.close()

} catch (SQLException e)

{

e.printStackTrace()

}

}

if(pstmt!=null)

{

try

{

pstmt.close()

} catch (SQLException e)

{

// TODO Auto-generated catch block

e.printStackTrace()

}

}

if(con!=null)

{

try

{

con.close()

} catch (SQLException e)

{

// TODO Auto-generated catch block

e.printStackTrace()

}

}

}

public Connection getCon()

{

return con

}

public void setCon(Connection con)

{

this.con = con

}

public PreparedStatement getPstmt()

{

return pstmt

}

public void setPstmt(PreparedStatement pstmt)

{

this.pstmt = pstmt

}

public ResultSet getRs()

{

return rs

}

public void setRs(ResultSet rs)

{

this.rs = rs

}

public String getDriver()

{

return driver

}

public void setDriver(String driver)

{

this.driver = driver

}

public String getDbname()

{

return dbname

}

public void setDbname(String dbname)

{

this.dbname = dbname

}

public String getUsername()

{

return username

}

public void setUsername(String username)

{

this.username = username

}

public String getPassword()

{

return password

}

public void setPassword(String password)

{

this.password = password

}

public String getUrl()

{

return url

}

public void setUrl(String url)

{

this.url = url

}

}


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

原文地址:https://54852.com/sjk/6775514.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存