推荐于2017-12-16
展开全部
package 数据库测试;
import java.sql.*;
/**
* @author qingsongwang
* @2008.11.16
*
* 说明:实现与数据库相连,取出数据库的内容显示
*/
public class JDBCTest
{
//主函数main()
public static void main(String[] args) throws Exception
{
String kongge=new String(" ");
//为后面的结果集输出好看点
Class.forName("com.mysql.jdbc.Driver");
//驱动
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/greatwqs?user=root&password=greatwqs");
/*连接数据库,jdbc:mysql://localhost:3306/greatwqs 数据库为greatwqs数据库
* 端口为3306
*
* 用户名user=root
*
* 用户密码password=greatwqs
*/
Statement stmt=conn.createStatement();
//创建SQL语句,实现对数据库的操作功能
ResultSet rs=stmt.executeQuery("select * from person");
//返回查询的结果
while(rs.next())
{
System.out.print(rs.getString("id")+kongge);
System.out.print(rs.getString("name")+kongge);
System.out.print(rs.getString("gender")+kongge);
System.out.print(rs.getString("major")+kongge);
System.out.print(rs.getString("phone")+kongge);
System.out.println();
}//输出结果集的内容
rs.close();
stmt.close();
conn.close();
//关闭语句,结果集,数据库的连接.
}
}
我运行的结果如下
import java.sql.*;
/**
* @author qingsongwang
* @2008.11.16
*
* 说明:实现与数据库相连,取出数据库的内容显示
*/
public class JDBCTest
{
//主函数main()
public static void main(String[] args) throws Exception
{
String kongge=new String(" ");
//为后面的结果集输出好看点
Class.forName("com.mysql.jdbc.Driver");
//驱动
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/greatwqs?user=root&password=greatwqs");
/*连接数据库,jdbc:mysql://localhost:3306/greatwqs 数据库为greatwqs数据库
* 端口为3306
*
* 用户名user=root
*
* 用户密码password=greatwqs
*/
Statement stmt=conn.createStatement();
//创建SQL语句,实现对数据库的操作功能
ResultSet rs=stmt.executeQuery("select * from person");
//返回查询的结果
while(rs.next())
{
System.out.print(rs.getString("id")+kongge);
System.out.print(rs.getString("name")+kongge);
System.out.print(rs.getString("gender")+kongge);
System.out.print(rs.getString("major")+kongge);
System.out.print(rs.getString("phone")+kongge);
System.out.println();
}//输出结果集的内容
rs.close();
stmt.close();
conn.close();
//关闭语句,结果集,数据库的连接.
}
}
我运行的结果如下
2013-05-23
展开全部
package database;
import java.sql.*;
/**
* @author Administrator
*
*/
public class DBAccess {
private Connection m_conn;
private Statement m_stmt;
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=sc";
String uName = "sa";
String uPwd = "sa";
/**
*
*/
public DBAccess() {
this.setDriver(driver);
this.setConnection(url, uName, uPwd);
}
public DBAccess(String driver, String url, String userName, String userPWD) {
try {
m_conn = DriverManager.getConnection(url, userName, userPWD);
m_stmt = m_conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
public boolean setDriver(String driver) {
try {
Class.forName(driver);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean setConnection(String url, String userName, String userPWD) {
try {
m_conn = DriverManager.getConnection(url, userName, userPWD);
m_stmt = m_conn.createStatement();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// 处理查询
public ResultSet sendQuery(String sql) {
try {
ResultSet m_rs = m_stmt.executeQuery(sql);
return m_rs;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
// 处理数据更新
public int sendUpdate(String sql) {
try {
return m_stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
return -1;
}
}
// 测试程序
public static void main(String[] arg) {
DBAccess db = new DBAccess();
String sql = "select * from Student";
ResultSet rs = db.sendQuery(sql);
try {
if (rs != null) {
while (rs.next()) {
System.out.println(rs.getInt("Sno") + " "
+ rs.getString("Sname"));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
import java.sql.*;
/**
* @author Administrator
*
*/
public class DBAccess {
private Connection m_conn;
private Statement m_stmt;
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=sc";
String uName = "sa";
String uPwd = "sa";
/**
*
*/
public DBAccess() {
this.setDriver(driver);
this.setConnection(url, uName, uPwd);
}
public DBAccess(String driver, String url, String userName, String userPWD) {
try {
m_conn = DriverManager.getConnection(url, userName, userPWD);
m_stmt = m_conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
}
public boolean setDriver(String driver) {
try {
Class.forName(driver);
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public boolean setConnection(String url, String userName, String userPWD) {
try {
m_conn = DriverManager.getConnection(url, userName, userPWD);
m_stmt = m_conn.createStatement();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// 处理查询
public ResultSet sendQuery(String sql) {
try {
ResultSet m_rs = m_stmt.executeQuery(sql);
return m_rs;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
// 处理数据更新
public int sendUpdate(String sql) {
try {
return m_stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
return -1;
}
}
// 测试程序
public static void main(String[] arg) {
DBAccess db = new DBAccess();
String sql = "select * from Student";
ResultSet rs = db.sendQuery(sql);
try {
if (rs != null) {
while (rs.next()) {
System.out.println(rs.getInt("Sno") + " "
+ rs.getString("Sname"));
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询