怎么连接数据库,用JAVA?麻烦大哥们写一下代码吧,拜托了。。。
在做登陆界面时候,我希望只有我的数据库里用户才能登陆进去的。publicclassTest...{publicstaticvoidmain(String[]args).....
在做登陆界面时候,我希望只有我的数据库里用户才能登陆进去的。
public class Test ...{
public static void main(String[] args) ...{
// 使用jdbc-odbc桥连接SQL Server数据库
String sql;
Connection conn;
Statement stmt;
ResultSet rs;
String url = "jdbc:odbc:pubs";
String user = "sa";
String password = "sa";
try ...{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
sql = "select * from employee";
rs = stmt.executeQuery(sql);
int i = 0;
while (rs.next()) ...{
System.out.println("employee" + (i+1) + "'s " + "name is: " + rs.getString(2));
i++;
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) ...{
e.printStackTrace();
}
}
}
这样对吗 展开
public class Test ...{
public static void main(String[] args) ...{
// 使用jdbc-odbc桥连接SQL Server数据库
String sql;
Connection conn;
Statement stmt;
ResultSet rs;
String url = "jdbc:odbc:pubs";
String user = "sa";
String password = "sa";
try ...{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
sql = "select * from employee";
rs = stmt.executeQuery(sql);
int i = 0;
while (rs.next()) ...{
System.out.println("employee" + (i+1) + "'s " + "name is: " + rs.getString(2));
i++;
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) ...{
e.printStackTrace();
}
}
}
这样对吗 展开
3个回答
展开全部
//JDBC与ODBC方法均有
package org;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Conn{
//private String DBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
//private String DBurl="jdbc:odbc:SIMS";
private String DBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String DBurl="jdbc:microsoft:sqlserver://localhost:1433;databaseName=first";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
private String DBuser="sa";
private String DBpwd="sa";
public Conn()
{
try
{
Class.forName(DBDriver);
System.out.println("驱动加载成功.....");
}
catch(Exception e)
{
System.out.println("数据库加载失败!!!");
e.printStackTrace();
}
try
{
con=DriverManager.getConnection(DBurl,DBuser,DBpwd);
System.out.println("数据库连接成功!!!");
}
catch(Exception e)
{
System.out.println("数据库连接失败!!!!!");
e.printStackTrace();
}
try
{
stmt=con.createStatement();
System.out.println("数据库声明成功!!!!");
}
catch(Exception e)
{
System.out.println("数据库创建声明失败!!!!");
e.printStackTrace();
}
}
/**
* 执行查询功能
*/
public ResultSet Query(String sql)
{
try
{
rs=stmt.executeQuery(sql);
System.out.println("执行查询成功!!!");
}
catch(Exception e)
{
System.out.println("执行查询出现异常!!!!");
e.printStackTrace();
}
return rs;
}
/**
* 执行更新操作
*/
public int Update(String sql)
{
int i=0;
try
{
i=stmt.executeUpdate(sql);
System.out.println("执行更新成功!!!!");
}
catch(Exception e)
{
System.out.println("执行更新失败!!!!");
e.printStackTrace();
}
return i;
}
/**
* 关闭数据库(ResultSet)的连接
*/
public void rsClose()
{
try
{
rs.close();
System.out.println("数据库结果集成功关闭!!!");
}
catch(Exception e)
{
e.printStackTrace();
System.out.print("数据库结果集关闭失败");
}
}
public void close()
{
try
{
con.close();
System.out.println("数据库成功关闭!!!!");
}
catch(Exception e)
{
System.out.println("关闭数据库时出现异常!!!!");
e.printStackTrace();
}
}
}
package org;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Conn{
//private String DBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
//private String DBurl="jdbc:odbc:SIMS";
private String DBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String DBurl="jdbc:microsoft:sqlserver://localhost:1433;databaseName=first";
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
private String DBuser="sa";
private String DBpwd="sa";
public Conn()
{
try
{
Class.forName(DBDriver);
System.out.println("驱动加载成功.....");
}
catch(Exception e)
{
System.out.println("数据库加载失败!!!");
e.printStackTrace();
}
try
{
con=DriverManager.getConnection(DBurl,DBuser,DBpwd);
System.out.println("数据库连接成功!!!");
}
catch(Exception e)
{
System.out.println("数据库连接失败!!!!!");
e.printStackTrace();
}
try
{
stmt=con.createStatement();
System.out.println("数据库声明成功!!!!");
}
catch(Exception e)
{
System.out.println("数据库创建声明失败!!!!");
e.printStackTrace();
}
}
/**
* 执行查询功能
*/
public ResultSet Query(String sql)
{
try
{
rs=stmt.executeQuery(sql);
System.out.println("执行查询成功!!!");
}
catch(Exception e)
{
System.out.println("执行查询出现异常!!!!");
e.printStackTrace();
}
return rs;
}
/**
* 执行更新操作
*/
public int Update(String sql)
{
int i=0;
try
{
i=stmt.executeUpdate(sql);
System.out.println("执行更新成功!!!!");
}
catch(Exception e)
{
System.out.println("执行更新失败!!!!");
e.printStackTrace();
}
return i;
}
/**
* 关闭数据库(ResultSet)的连接
*/
public void rsClose()
{
try
{
rs.close();
System.out.println("数据库结果集成功关闭!!!");
}
catch(Exception e)
{
e.printStackTrace();
System.out.print("数据库结果集关闭失败");
}
}
public void close()
{
try
{
con.close();
System.out.println("数据库成功关闭!!!!");
}
catch(Exception e)
{
System.out.println("关闭数据库时出现异常!!!!");
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public boolean logon(YongHuDBbean yongHuDBbean) throws ClassNotFoundException, SQLException
{
int i = 0;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=shop","sa","sa");
pstmt = conn.prepareStatement("select name,password from yonghu where name = ? and password = ?");
pstmt.setString(1,yongHuDBbean.name);
pstmt.setString(2,yongHuDBbean.password);
rs = pstmt.executeQuery();
if(rs.next())
{
return true;
}
return false;
}
YongHuDBbean 里封装了 用户表里的自段
你首先把 从前台 里取得的用户名密码 存放到一个YongHuDBbean 里,然后把这个YonghuDBbean 传到上面的方法里 就能判断是否登陆成功 YonghuDbBEAN的代码在下面
NAME,PASSWORD是前台传过来的用户名密码
public class YongHuDBbean
{
private String name;
private String password;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
{
int i = 0;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=shop","sa","sa");
pstmt = conn.prepareStatement("select name,password from yonghu where name = ? and password = ?");
pstmt.setString(1,yongHuDBbean.name);
pstmt.setString(2,yongHuDBbean.password);
rs = pstmt.executeQuery();
if(rs.next())
{
return true;
}
return false;
}
YongHuDBbean 里封装了 用户表里的自段
你首先把 从前台 里取得的用户名密码 存放到一个YongHuDBbean 里,然后把这个YonghuDBbean 传到上面的方法里 就能判断是否登陆成功 YonghuDbBEAN的代码在下面
NAME,PASSWORD是前台传过来的用户名密码
public class YongHuDBbean
{
private String name;
private String password;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上的哥们写的基本上是正确的,如果要用你可以用他的代码!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询