当前位置: 技术问答>java相关
关于jdbc的问题!
来源: 互联网 发布时间:2015-05-29
本文导语: 请问各位大虾,java怎么样和sql Server的数据库连接?还用得着在odbc里设置数据源吗?最好能给我一个实例!:-) | 需要在系统中配置数据源 import java.net.*; import java.sql.*; class jdbcexample { publi...
请问各位大虾,java怎么样和sql Server的数据库连接?还用得着在odbc里设置数据源吗?最好能给我一个实例!:-)
|
需要在系统中配置数据源
import java.net.*;
import java.sql.*;
class jdbcexample
{
public static void main (String args[])
{int i;
String URL="jdbc:odbc:数据源名";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(URL,"user","password");
checkForWarning(con.getWarnings());
DatabaseMetaData dma=con.getMetaData();
System.out.println("nConnected to"+dma.getURL());
System.out.println("Driver "+dma.getDriverName());
System.out.println("Version "+dma.getDriverVersion());
System.out.println("");
Statement stmt=con.createStatement();
//stmt.executeUpdate("CREATE TABLE two (ID INTEGER, Name VARCHAR(10));");
//stmt.executeUpdate("INSERT INTO two VALUES (10,'xu');");
//stmt.executeUpdate("INSERT INTO two (ID,Name) VALUES (20,'du');");
//stmt.executeUpdate("INSERT INTO two (ID,Name) VALUES (50,'neo');");
//stmt.executeUpdate("INSERT INTO two (ID,Name) VALUES (40,'liu');");
ResultSet rs=stmt.executeQuery("SELECT * FROM two;");
ResultSetMetaData rsmd=rs.getMetaData();
int numCols=rsmd.getColumnCount();
System.out.println("Result of SELET * FROM one");
for(i=1;i1) System.out.print(",");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");
while (rs.next())
{
for(i=1;i1) System.out.print(",");
if(rs.wasNull()) System.out.print("NULL");
else System.out.print(rs.getString(i));
}
}
System.out.println("");
con.close();
}
catch(SQLException ex)
{
System.out.println("n* * * SQLException caught * * * n");
while (ex!=null)
{
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message: "+ex.getMessage());
System.out.println("Vendor: "+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
catch(java.lang.Exception ex)
{
ex.printStackTrace();
}
}
private static boolean checkForWarning (SQLWarning warn)
throws SQLException
{
boolean rc=false;
if (warn!=null)
{
System.out.println("n * * * Warning * * * n");
rc=true;
while(warn!=null)
{
System.out.println("SQLState:"+warn.getSQLState());
System.out.println("Message: "+warn.getMessage());
System.out.println("Vendor: "+warn.getErrorCode());
System.out.println("");
warn=warn.getNextWarning();
}
}
return rc;
}
}
import java.net.*;
import java.sql.*;
class jdbcexample
{
public static void main (String args[])
{int i;
String URL="jdbc:odbc:数据源名";
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(URL,"user","password");
checkForWarning(con.getWarnings());
DatabaseMetaData dma=con.getMetaData();
System.out.println("nConnected to"+dma.getURL());
System.out.println("Driver "+dma.getDriverName());
System.out.println("Version "+dma.getDriverVersion());
System.out.println("");
Statement stmt=con.createStatement();
//stmt.executeUpdate("CREATE TABLE two (ID INTEGER, Name VARCHAR(10));");
//stmt.executeUpdate("INSERT INTO two VALUES (10,'xu');");
//stmt.executeUpdate("INSERT INTO two (ID,Name) VALUES (20,'du');");
//stmt.executeUpdate("INSERT INTO two (ID,Name) VALUES (50,'neo');");
//stmt.executeUpdate("INSERT INTO two (ID,Name) VALUES (40,'liu');");
ResultSet rs=stmt.executeQuery("SELECT * FROM two;");
ResultSetMetaData rsmd=rs.getMetaData();
int numCols=rsmd.getColumnCount();
System.out.println("Result of SELET * FROM one");
for(i=1;i1) System.out.print(",");
System.out.print(rsmd.getColumnLabel(i));
}
System.out.println("");
while (rs.next())
{
for(i=1;i1) System.out.print(",");
if(rs.wasNull()) System.out.print("NULL");
else System.out.print(rs.getString(i));
}
}
System.out.println("");
con.close();
}
catch(SQLException ex)
{
System.out.println("n* * * SQLException caught * * * n");
while (ex!=null)
{
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("Message: "+ex.getMessage());
System.out.println("Vendor: "+ex.getErrorCode());
ex=ex.getNextException();
System.out.println("");
}
}
catch(java.lang.Exception ex)
{
ex.printStackTrace();
}
}
private static boolean checkForWarning (SQLWarning warn)
throws SQLException
{
boolean rc=false;
if (warn!=null)
{
System.out.println("n * * * Warning * * * n");
rc=true;
while(warn!=null)
{
System.out.println("SQLState:"+warn.getSQLState());
System.out.println("Message: "+warn.getMessage());
System.out.println("Vendor: "+warn.getErrorCode());
System.out.println("");
warn=warn.getNextWarning();
}
}
return rc;
}
}
|
如果用数据源的话,就可以不用下载专门的数据库驱动了
在控制面版-管理工具-数据源-系统DNS里面配置要访问的数据库对应数据源。
假设你的数据源的名称为Test;用户sa,密码admin
则连接代码如下:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Test", "sa", "admin");
==============================
在下所说,也许并非正确,请从建议的角度批判地吸收!
在控制面版-管理工具-数据源-系统DNS里面配置要访问的数据库对应数据源。
假设你的数据源的名称为Test;用户sa,密码admin
则连接代码如下:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:Test", "sa", "admin");
==============================
在下所说,也许并非正确,请从建议的角度批判地吸收!