📄
字号:
例12.1 TestJDBC_1.java源代码:
//TestJDBC_1.java
import java.sql.*;
public class TestJDBC_1{
public static void main(String[] args) {
String jdbcDriver="sun.jdbc.odbc.JdbcOdbcDriver"; // 数据库驱动
String jdbcURL="jdbc:odbc:Student"; // 数据源
try{
Class.forName(jdbcDriver); //注册驱动程序
Connection con = DriverManager.getConnection(jdbcURL);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from TestStudent");
System.out.println("-------------------------------------------------");
while(rs.next()){
System.out.print(" 号码:" + rs.getInt(1));
System.out.print(" 姓名:" + rs.getString(2)+ "\t");
System.out.print(" 性别:" + rs.getString(3));
System.out.print(" 年龄:" + rs.getInt(4));
System.out.println();
}
rs.close();
stmt.close();
con.close();
System.out.println("-------------------------------------------------");
System.out.print("Test OK!");
} catch(ClassNotFoundException e){
System.out.println("未找到要加载的驱动类");
} catch(SQLException e) {
System.out.println("SQL错误提示:" + e);
}
}
}
例12.2 TestJDBC_2.java的源代码
//TestJDBC_2.java
import java.sql.*;
public class TestJDBC_2 {
public static void main(String[] args) {
String jdbcDriver="sun.jdbc.odbc.JdbcOdbcDriver"; //数据库驱动
String jdbcURL="jdbc:odbc:Student"; //数据源
try{
Class.forName(jdbcDriver); //注册驱动程序
Connection con = DriverManager.getConnection(jdbcURL);
PreparedStatement pstmt =
con.prepareStatement("INSERT INTO TestStudent(学号, 姓名, 性别,
年龄, 专业) VALUES(?, ?, ?, ?, ?)");
pstmt.setInt(1, 5);
pstmt.setString(2, "刘枫");
pstmt.setString(3, "男");
pstmt.setInt(4, 23);
pstmt.setString(5, "经济管理");
pstmt.executeUpdate();
System.out.print("添加成功");
pstmt.close();
con.close();
} catch(ClassNotFoundException e){
System.out.println("未找到要加载的驱动类");
} catch(SQLException e) {
System.out.println("SQL错误提示:" + e);
}
}
}
例12.3 SQL_JDBC.java的源代码
//SQL_JDBC.java
import java.sql.*;
public class SQL_JDBC {
public static void main(String[] args) {
Connection conn;
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs";
String user = "sa"; // SQL数据库的用户名
String pwd = ""; // SQL用户名的密码
try {
Class.forName(driver); // 注册驱动
conn = DriverManager.getConnection(url, user, pwd); // 建立连接
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// 设置为可滚动的查询对象
String sql="select * from jobs"; // 查询jobs表
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {
System.out.print(" " + rs.getInt(1));
System.out.println("\t" + rs.getString(2));
}
stmt.close();
conn.close();
System.out.println("\n" + "Test OK!");
} catch(ClassNotFoundException e){
System.out.println("未找到要加载的驱动类");
} catch(SQLException e) {
System.out.println("SQL错误提示:" + e);
}
}
}
例12.4 源代码
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class MyPanel extends JPanel implements ActionListener {
JLabel lblUserName;
JTextField tfUserID;
JLabel lblPwd;
JPasswordField tfPwd;
JButton submit;
JButton quit;
JFrame frame;
/// 构造方法 \\\
public MyPanel() {
lblUserName = new JLabel("用户名:");
tfUserID = new JTextField(5);
lblPwd = new JLabel("密码");
tfPwd = new JPasswordField(10);
submit = new JButton("提交");
quit = new JButton("退出");
add(lblUserName);
add(tfUserID);
add(lblPwd);
add(tfPwd);
add(submit);
add(quit);
submit.addActionListener(this);
quit.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
int flag = 0;
String userName = tfUserID.getText();
String password = tfPwd.getText();
if (e.getSource() == quit ) {
System.exit(0);
} else if (e.getSource() == submit && userName != null && password!=null ) {
Connection conn;
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=pubs";
String user = "sa"; // SQL数据库的用户名
String pwd = ""; // SQL用户名的密码
try {
Class.forName(driver); // 注册驱动
conn = DriverManager.getConnection(url, user, pwd); // 建立连接
Statement stmt=
conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// 设置为可滚动的查询对象
String sql="select * from Admin where userName='"
+ userName + "' AND password='"
+ password + "'";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next())
JOptionPane.showMessageDialog(null, "登陆成功");
else
JOptionPane.showMessageDialog(null, "登陆失败", "警告",
JOptionPane.WARNING_MESSAGE);
stmt.close();
conn.close();
} catch(ClassNotFoundException er){
System.out.println("未找到要加载的驱动类");
} catch(SQLException err) {
System.out.println("SQL错误提示:" + err);
}
}
}
}
class MyFrame extends JFrame {
/// 构造方法 \\\
public MyFrame() {
super("登陆测试");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(200, 150);
setSize(400,300);
this.getContentPane().setLayout(new FlowLayout());
Container contentPane = getContentPane();
contentPane.add(new MyPanel());
}
}
public class Login {
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new MyFrame();
frame.pack();
frame.show();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -