Java Swing窗体实现影院售票系统
实现思路
一、数据库
二、Java连接数据库并执行sql
1、编写DBHelper
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBHelper {
private static String className = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://localhost:3306/movies_db?characterEncoding=utf-8";
private static String user = "root";
private static String password = "root";
//加载驱动
static{
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url, user, password);
}
//关闭链接
public static void close(Connection conn){
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2、MovieDao
编写前先说些Movie实体类
package com.sdgy;
public class Movie {
private int id;
private String name;
private int duration;
private String area;
private String moviedate;
private double price;
public Movie() {
// TODO Auto-generated constructor stub
}
public Movie(int id, String name, int duration,