java怎样存数据到mysql数据库

java怎样存数据到mysql数据库,第1张

用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

}

}

import java.sql.Connection 

import java.sql.DriverManager 

import java.sql.Statement 

public class InsertDemo01{

// 定义MySQL的数据库驱动程序

public static final String DBDRIVER = "org.gjt.mm.mysql.Driver"

// 定义MySQL数据库的连接地址

public static final String DBURL = "jdbc:mysql://localhost:3306/mldn"

// MySQL数据库的连接用户名

public static final String DBUSER = "root"

// MySQL数据库的连接密码

public static final String DBPASS = "mysqladmin"

public static void main(String args[]) throws Exception {   // 所有的异常抛出

Connection conn = null               // 数据库连接

Statement stmt = null                      // 数据库 *** 作

Class.forName(DBDRIVER)       // 加载驱动程序

String sql = "INSERT INTO user(name,password,age,sex,birthday) "+

" VALUES ('李兴华','www.mldn.cn',30,'男','2008-08-27')"

conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS)

stmt = conn.createStatement()  // 实例化Statement对象

stmt.executeUpdate(sql)           // 执行数据库更新 *** 作

stmt.close()                                // 关闭 *** 作

conn.close()                 // 数据库关闭

}

}


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

原文地址: http://outofmemory.cn/zaji/6119321.html

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

发表评论

登录后才能评论

评论列表(0条)

保存