用java连接数据库啊。下面给点例子可以参考,不大好,凑合看
import java.sql.Connectionimport 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
}
}
下面是一个从 mysql 数据库获取用户信息的例子,可以参考一下:
import java.sql.Connectionimport java.sql.DriverManager
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Statement
import java.util.ArrayList
import java.util.List
// 用户类,存储单个用户信息
class User {
private int id
private String name
public User(int id, String name) {
this.id = id
this.name = name
}
public int getId() {
return id
}
public void setId(int id) {
this.id = id
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + "]"
}
}
public class Demo1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
// 本例使用 mysql 数据库,演示将数据库 test 的 tb_users 表中的用户信息
// 放到 List 中
// 加载数据驱动
Class.forName("com.mysql.jdbc.Driver")
// 数据库连接字符串, 此例数据库为 test
String url = "jdbc:mysql://localhost:3306/test"
String user = "root" // 数据库用户名
String password = "" // 数据库密码
// 打开一个数据连接
Connection conn = DriverManager.getConnection(url, user, password)
Statement stmt = conn.createStatement()
// 获取表 tb_users 所有用户信息到结果集中
ResultSet rs = stmt.executeQuery("SELECT id, name FROM tb_users")
// 定义一个存放用户信息的 List
List<User> users = new ArrayList<>()
// 提取用户信息,并将用户信息放入 List
while (rs.next()) {
// 获取用户ID
int id = rs.getInt(1)
// 获取用户名
String name = rs.getString(2)
users.add(new User(id, name))
}
rs.close()
stmt.close()
conn.close()
// 显示用户信息
for (User u : users) {
System.out.println(u)
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)