IDEA sqlserver database connection
Database Configuration
Install sqlserver. Links: sqlserver database engine , management tools . Installation ...... find their own tutorial.
log-in name
1. Log on to verify with Windows
2. Open the login name> security, find or create a new login name
right login name, password
3. Log back management tool, SQL Server authentication method, login name, password is correct then the login is successful
Create a database in which to test the new people table
Port Settings
1. Right My Computer -> Manage -> Services and Applications -> SQL server network configuration
2. Locate the TCP / IP, double-click to open the IP Address tab, and configure
3. Restart the SQL Server service
Download JDBC package
Download JDBC and unzip to any location
Introducing the jar package IDEA
1. Create a new project, create a test class
public class Test {
private static Connection dbConn = null;
public static void main(String[] args) {
String dbURL = "jdbc:sqlserver://192.168.1.125:1433;DatabaseName=sa";
try {
//1.加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("加载驱动成功!");
//2.连接
dbConn = DriverManager.getConnection(dbURL, "sa", "123");
System.out.println("连接数据库成功!");
String sql="select * from people";
PreparedStatement statement=null;
statement=dbConn.prepareStatement(sql);
ResultSet res=null;
res=statement.executeQuery();
while(res.next()){
String title=res.getString("name");
System.out.println(title);
}
}catch(Exception e) {
e.printStackTrace();
System.out.println("连接数据库失败!");
}
}
}
2. Press Ctrl + Alt + shift + S or open File-> Project Structure, open the Dependencies tab, add the following jar package
3. Press Ctrl + shift + F10 to start the program, the following results
加载驱动成功!
连接数据库成功!
王五
李四
张三
Reading success