临近毕业,用java做了个学生信息管理系统,需要用到sql数据库,请问怎么连接,急用,谢谢各位,
package test;
import java.sql.*;
import cn.com.sdata.util.SDGetConnection;
/**
*
* @author user
*mssql数据库连接操作
*/
public class MssqlConnTest {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://127.0.0.1:1433; DatabaseName=test"; //连接服务器和数据库sample
String userName = "sa"; //默认用户名
String userPwd = "000000"; //密码
Connection dbConn;
public Connection getConnection()
{
try
{
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!
return dbConn;
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public void closeConnection()
{
try {
dbConn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] srg)
{
// DBConnectionManager DBConnectionManager1 = new DBConnectionManager();
try
{
System.out.println("success");
MssqlConnTest test = new MssqlConnTest();
Connection conn = test.getConnection();
System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select top 1 * from DOI");
while(rs.next())
{
System.out.print("\tfirst: "+rs.getString(1));
System.out.print("\tsecond: "+rs.getString(2));
}
//*/
}catch(Exception e)
{
e.printStackTrace();
}
}
}
import java.sql.*;
import cn.com.sdata.util.SDGetConnection;
/**
*
* @author user
*mssql数据库连接操作
*/
public class MssqlConnTest {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://127.0.0.1:1433; DatabaseName=test"; //连接服务器和数据库sample
String userName = "sa"; //默认用户名
String userPwd = "000000"; //密码
Connection dbConn;
public Connection getConnection()
{
try
{
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!
return dbConn;
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public void closeConnection()
{
try {
dbConn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] srg)
{
// DBConnectionManager DBConnectionManager1 = new DBConnectionManager();
try
{
System.out.println("success");
MssqlConnTest test = new MssqlConnTest();
Connection conn = test.getConnection();
System.out.println("Connection Successful!"); //如果连接成功 控制台输出Connection Successful!
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select top 1 * from DOI");
while(rs.next())
{
System.out.print("\tfirst: "+rs.getString(1));
System.out.print("\tsecond: "+rs.getString(2));
}
//*/
}catch(Exception e)
{
e.printStackTrace();
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答 2011-04-16
主要用四步:
1加载驱动Class.forName("oracle.jdbc.driver.OracleDriver");
数据库不同,驱动也不同,这是Oracled
2建立连接
conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
3执行获得结果集
stmt = conn.createStatement();
rs = stmt.executeQuery("sql语句")
遍历结果集
while(rs.next()) {
rs.getString("列名")
......
}
1加载驱动Class.forName("oracle.jdbc.driver.OracleDriver");
数据库不同,驱动也不同,这是Oracled
2建立连接
conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.1:1521:SXT", "scott", "tiger");
3执行获得结果集
stmt = conn.createStatement();
rs = stmt.executeQuery("sql语句")
遍历结果集
while(rs.next()) {
rs.getString("列名")
......
}
第2个回答 2011-04-15
使用Connection c = DriverManager.getConnerction(username ,password, url);
第3个回答 2011-04-15
常用的数据库有3个 SQL sever , mysql , oracle......不知道你说的是哪 个??? ,建议学下DButil