常用數據庫的鏈接
package test;
import Java.sql.*;
public class TestDataBase {
public static void main(String[] args) {
//driver接口
//com.microsoft.jdbc.sqlserver.SQLServerDriver
//Oracle.jdbc.driver.OracleDriver
String driver = "com.MySQL.jdbc.Driver";
//url
//jdbc:microsoft:sqlserver://localhost:1433;databasename = pubs
//jdbc:Oracle:thin:@localhost:1521:ORCL
String url = "jdbc:MySQL://127.0.0.1:3306/menagerIE";
String user = "root";
String passWord = "11111";
try
{
Class.forName(driver);
Connection con = DriverManager.getConnection(url,user,passWord);
String selectString = "select * from pet;";
PreparedStatement prestatement = con.prepareStatement(selectString);
ResultSet result = prestatement.executeQuery(selectString);
while(result.next())
{
System.out.println(result.getString(1) + "t" + result.getString(2));
}
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
}
}