数据库

package com.util;

import java.sql.*;

/**
* 数据库工具
*/
public class DBUtil {
private static String dbUrl = "jdbc:mysql://localhost:3306/javawebdemo"; //数据库地址
private static String dbUser = "root"; //数据库用户
private static String dbPwd = "123456"; //数据库密码
private static String dbDriver = "com.mysql.jdbc.Driver"; //数据库驱动

private static Connection conn = null;

//获取连接
public static Connection getConn(){
if(null == conn){
try {
Class.forName(dbDriver);
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}

/*//测试
@Test
public void test(){
if(getConn() != null){
System.out.println("数据库连接成功");
}else{
System.out.println("数据库连接失败");
}
}*/
}

package com.domain;

/**
* 用户实体类
*/
public class User {
private int id;
private String username;
private String password;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

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;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}

posted @ 2024-02-23 15:17  涨涨涨张  阅读(3)  评论(0编辑  收藏  举报