3个回答
展开全部
import java.sql.*;
public class DB_Conn {
public static Connection getConnect()
{
String driver = "com.mysql.jdbc.Driver";
// URL指向要访问的数据库名scutcs
String url = "jdbc:mysql://127.0.0.1:3306/xueshengxinxi";
// MySQL配置时的用户名
String user = "root";
Connection conn=null;
// Java连接MySQL配置时的密码
String password = "123456";
try {
// 加载驱动程序
Class.forName(driver);
// 连续数据库
conn = DriverManager.getConnection(url, user, password);
}catch(Exception e)
{
e.printStackTrace();
System.out.println("连接失败");
}
return conn;
}
public static Statement sm(Connection conn)
{
Statement sm=null;
try {
sm = conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sm;
}
public DB_Conn()
{
DB_Conn.getConnect();
}
}
public User get(String username)
{
String sql="select *from userinfo where username='"+username+"'";
ResultSet rs=null;
try {
rs=DB_Conn.sm(db_conn).executeQuery(sql);
System.out.println(rs.next());
if(!rs.next())
{
return null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new User();
}
}
向前面回答的一样自己看看jdbc给你一个我写过的参考一下
public class DB_Conn {
public static Connection getConnect()
{
String driver = "com.mysql.jdbc.Driver";
// URL指向要访问的数据库名scutcs
String url = "jdbc:mysql://127.0.0.1:3306/xueshengxinxi";
// MySQL配置时的用户名
String user = "root";
Connection conn=null;
// Java连接MySQL配置时的密码
String password = "123456";
try {
// 加载驱动程序
Class.forName(driver);
// 连续数据库
conn = DriverManager.getConnection(url, user, password);
}catch(Exception e)
{
e.printStackTrace();
System.out.println("连接失败");
}
return conn;
}
public static Statement sm(Connection conn)
{
Statement sm=null;
try {
sm = conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sm;
}
public DB_Conn()
{
DB_Conn.getConnect();
}
}
public User get(String username)
{
String sql="select *from userinfo where username='"+username+"'";
ResultSet rs=null;
try {
rs=DB_Conn.sm(db_conn).executeQuery(sql);
System.out.println(rs.next());
if(!rs.next())
{
return null;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new User();
}
}
向前面回答的一样自己看看jdbc给你一个我写过的参考一下
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询