This article introduces the Java Database Connection Program:
1. Direct Connection, no need to configure any drivers or anything, and no need to set anything. I use MyEclipse 8.0, SQL 2005 import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Public class MyLink {
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Try {
Class. forName ("sun. jdbc. odbc. JdbcOdbcDriver ")
. NewInstance ();
String url = "jdbc: odbc: Driver = {SQL Server}; Server = localhost; Database = Component ";
// Mydb is a database
String user = "sa ";
String password = "sa ";
Connection conn = DriverManager. getConnection (url, user, password );
PreparedStatement partnames = conn. prepareStatement ("SELECT * FROM Part_Name ");
ResultSet rs1_partnames.exe cuteQuery ();
While (rs. next ())
{
String Data = rs. getString ("ACL_Part_Name"); // read Data from a column. ACL_Part_Name is the column name.
System. out. println ();
}
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
}
}
There is also a bridge, that is, to configure the system's data source and so on, also need to change the driver in ForName to change:
Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver"); // register the driver
Con = DriverManager. getConnection ("jdbc: microsoft: sqlserver: // your IP: 1433; databaseName = database name", "sa", "your password ");//