当前位置: 技术问答>java相关
java连接SQLSERVER数据库和执行SQL语句?
来源: 互联网 发布时间:2015-08-01
本文导语: 我是JAVA菜鸟,请教高手如何java连接SQLSERVER数据库和执行SQL语句?或者告诉我哪里有相关的内容也行啊,拜托! | import java.sql.*; import java.util.Properties; public class MakingAsStatement { public static ...
我是JAVA菜鸟,请教高手如何java连接SQLSERVER数据库和执行SQL语句?或者告诉我哪里有相关的内容也行啊,拜托!
|
import java.sql.*;
import java.util.Properties;
public class MakingAsStatement
{
public static void main(String [] args)
{
//load driver class
// String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
// String sourceURL="jdbc:odbc:test";
String sourceURL="jdbc:microsoft:sqlserver://hostname:1433";
try
{
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName (driverName);
Properties prop=new Properties();
prop.setProperty("user","sa") ;
prop.setProperty("password","pwd");
//define the data source for chiver
// String sourceURL="jdbc:odbc:test";
//create connection through the DriverManager
// Connection databaseConnection=DriverManager.getConnection(sourceURL);
Connection databaseConnection=DriverManager.getConnection(sourceURL,prop);
Statement statement=databaseConnection.createStatement();
ResultSet authorNames=statement.executeQuery(
"SELECT au_lname,au_fname FROM pubs.dbo.authors ORDER BY au_lname DESC,au_fname DESC");
ResultSetMetaData metaData=authorNames.getMetaData();
int columns=metaData.getColumnCount();
System.out.println("columns="+columns);
int row=0;
while (authorNames.next())
{
System.out.print("nRow"+(++row)+":");
for (int i=1;i
import java.util.Properties;
public class MakingAsStatement
{
public static void main(String [] args)
{
//load driver class
// String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
// String sourceURL="jdbc:odbc:test";
String sourceURL="jdbc:microsoft:sqlserver://hostname:1433";
try
{
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Class.forName (driverName);
Properties prop=new Properties();
prop.setProperty("user","sa") ;
prop.setProperty("password","pwd");
//define the data source for chiver
// String sourceURL="jdbc:odbc:test";
//create connection through the DriverManager
// Connection databaseConnection=DriverManager.getConnection(sourceURL);
Connection databaseConnection=DriverManager.getConnection(sourceURL,prop);
Statement statement=databaseConnection.createStatement();
ResultSet authorNames=statement.executeQuery(
"SELECT au_lname,au_fname FROM pubs.dbo.authors ORDER BY au_lname DESC,au_fname DESC");
ResultSetMetaData metaData=authorNames.getMetaData();
int columns=metaData.getColumnCount();
System.out.println("columns="+columns);
int row=0;
while (authorNames.next())
{
System.out.print("nRow"+(++row)+":");
for (int i=1;i