Hi,
Im very new in this topic.
And now I want to read some data from a SQL Server with Java.
But I dont 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
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,
> Im very new in this topic.
> And now I want to read some data from a SQL Server with Java.
> But I dont 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
>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment