Re: JDBC und SQL Server
From: Tim (triggerNOSPAM_at_get2net.dk)
Date: 08/15/04
- Next message: Tim: "DateTime format during INSERT using MS JDBC driver"
- Previous message: Patrick Hennig: "JDBC und SQL Server"
- In reply to: Patrick Hennig: "JDBC und SQL Server"
- Messages sorted by: [ date ] [ thread ]
Date: Sun, 15 Aug 2004 22:12:41 +0200
Hi Patrick,
Here's some sample code:
Connection connection = null;
try {
String driverName = "jdbc:microsoft:sqlserver://";
String serverName = "127.0.0.1";
String portNumber = "1433";
String myDatabaseUrl = serverName + ":" + portNumber;
String myDatabaseName = "DatabaseName=Northwind";
String url = "jdbc:microsoft:sqlserver://" + myDatabaseUrl + ";" +
myDatabaseName;
String username = "username";
String password = "password";
// Load the JDBC driver
Class.forName(driverName);
// Create a connection to the database
connection = DriverManager.getConnection(url, username, password);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT CustomerId, CompanyName FROM
Customers");
while (rs.next()) {
System.out.println(rs.getString("CustomerId"));
System.out.println(rs.getString("CompanyName"));
}
rs.close();
} catch (ClassNotFoundException e) {
// Could not find the database driver
e.printStackTrace();
} catch (SQLException e) {
// Could not connect to the database
e.printStackTrace();
}
finally {
try {
connection.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
A great place to find all kinds of Java sample code is here:
http://javaalmanac.com/cgi-bin/search/find.pl?words=sql
- Tim
"Patrick Hennig" <patrick_hennig@licht-concept.de> wrote in message
news:%23mXDLUsgEHA.3944@tk2msftngp13.phx.gbl...
> Hi,
>
> Iīm very new in this topic.
>
> And now I want to read some data from a SQL Server with Java.
> But I donīt know how.
>
> I have now downloaded the new Driver form Microsoft and now I want to read
> data from the SQL Server with Java.
>
> Can someone give me a little example.
>
> Sincerly Patrick Hennig
>
>
- Next message: Tim: "DateTime format during INSERT using MS JDBC driver"
- Previous message: Patrick Hennig: "JDBC und SQL Server"
- In reply to: Patrick Hennig: "JDBC und SQL Server"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|