图书管理系统:
通过运行JAVA,JDBC,数据库等技术完成。
部分代码如下:
连接数据库的代码:
package application;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
public class ConnectionSQL {
public PreparedStatement ps=null;
public Connection ct=null;
public ConnectionSQL() {
// PreparedStatement ps=null;
// Connection ct=null;
try {
//1.加载驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
System.out.println("加载驱动成功!");
}catch(Exception e) {
e.printStackTrace();
System.out.println("加载驱动失败!");
}
try {
//2.连接
ct=DriverManager.getConnection
("jdbc:sqlserver://localhost:1433;DatabaseName=TuShuGuanLi","sa","479260");
System.out.println("连接数据库成功!");
}
catch(Exception e) {
e.printStackTrace();
System.out.println("连接数据库失败!");
}
}
public static void main(String[] args) {
ConnectionSQL c = new ConnectionSQL();
c.getConnection();
}
private void getConnection() {
// TODO Auto-generated method stub
}
}
注册代码:
package application;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
public class ZhuCe extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
GridPane pane = new GridPane(); //建立一个GridPane类型面板
pane.setAlignment(Pos.CENTER); //居中
pane.setHgap(5); //间距
pane.setVgap(10);
TextField T1 = new TextField();//建立文本框
TextField T2 = new TextField();
TextField T3 = new TextField();
TextField T4 = new TextField();
pane.add(new Label("编号"), 0, 0);//标签
pane.add(T1, 1, 0);
pane.add(new Label("用户名"), 0, 1);
pane.add(T2, 1, 1);
pane.add(new Label("密码"), 0, 2);
pane.add(T3, 1, 2);
pane.add(new Label("确认密码"), 0, 3);
pane.add(T4, 1, 3);
HBox hbox = new HBox(); //建立一个HBox类型面板
hbox.setSpacing(20);
hbox.setAlignment(Pos.CENTER);
Button L1 = new Button("提交"); //按钮
hbox.getChildren().addAll(L1); //把L1加入面板hbox
pane.add(hbox, 1, 4); //将hbox面板加入到Pane面板
// T1.setCursor(Cursor.HAND);
Scene scene = new Scene(pane,400,400);
primaryStage.setTitle("注册界面");
primaryStage.setScene(scene);
primaryStage.show();
L1.setCursor(Cursor.HAND);
L1.setOnAction((e)->{
ConnectionSQL con2 = new ConnectionSQL(); //连接数据库
try{
String str1 = T1.getText(); //赋值
String str2 = T2.getText();
String str3 = T3.getText();
String sql = "insert into koulin values(?,?,?)";//插入语句
PreparedStatement statement = con2.ct.prepareStatement(sql);//实现数据库语句
int str1f = Integer.parseInt(str1); //类型强制转换
statement.setInt(1, str1f);
statement.setString(2, str2);
statement.setString(3, str3);
statement.executeUpdate(); //执行数据库语句
System.out.println("注册成功");
Main open=new Main(); //界面跳转到Main
try
{
//Stage stage = new Stage();
open.start(new Stage());
primaryStage.hide();
}
catch (Exception e4)
{
e4.printStackTrace();
}
}catch (Exception e3){
System.out.println("注册出错");
//e3.printStackTrace();
}});
}
public static void main(String[] args){
Application.launch(args);
}
}
登录代码:
package application;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
public class Main extends Application {
private int i =0; //定义个变量
@Override
public void start(Stage primaryStage) {
try {
GridPane pane = new GridPane();//创建一个GridPane类型的面板
pane.setAlignment(Pos.CENTER); //面板内容整体对齐方式
pane.setHgap(5); //节点之间的间隔
pane.setVgap(10);
TextField t1 = new TextField(); //创建一个文本框
/* int b=2;
String str4f = String.valueOf(b);
t1.setText(str4f);*/
TextField t2 = new TextField();
pane.add(new Label("用户名"), 0, 0); //创建一个标签,加入到pane这个面板中(列,行)
pane.add(t1, 1, 0);
pane.add(new Label("密码"), 0, 1);
pane.add(t2, 1, 1);
HBox hbox = new HBox(); //创建一个hbox类型的面板
hbox.setSpacing(20);//两节点的水平间隔
hbox.setAlignment(Pos.CENTER);//面板内容整体对齐方式
Button L1 = new Button("登录");//创建一个按钮
Button L2 = new Button("注册");
hbox.getChildren().add(L1);//添加到hbox中
hbox.getChildren().add(L2);
pane.add(hbox, 1, 2);//把hbox加入到pane中
L1.setOnAction((e)->{ //按钮L1的事件处理器(即点击按钮执行其中的语句)
if(i!=3){
ConnectionSQL con2 = new ConnectionSQL();//连接数据库
try{
String str1 = t1.getText();
String str2 = t2.getText();
String sql = "select id from koulin where yonghuming=? and mima=?";//定义数据库语句
PreparedStatement statement = con2.ct.prepareStatement(sql);//定义PreparedStatement对象
statement.setString(1, str1);//参数设置(即1:表示第一个问号,即str1取代第一个问号;2:表示第二个问号)
statement.setString(2, str2);
ResultSet rest = statement.executeQuery();//定义返回值
if(rest.next()){ //有值则返回ture,执行以下语句(界面跳转语句)
L1.setCursor(Cursor.HAND);
Function open=new Function(); //跳转到Function类的界面
try
{
open.start(new Stage());
primaryStage.hide();
}
catch (Exception e1)
{
e1.printStackTrace();
}
}
else{
System.out.println("你是非法用户!");
i++;
}
}catch (Exception e1){
e1.printStackTrace();
// System.out.println("你是非法用户!");
}
}
else{
System.out.println("连续输入错误口令超过三次,关闭程序!");
System.exit(1); //退出程序
}
});
//System.exit(1);
Scene scene = new Scene(pane,900,900);//将pane加入到scene中(把面板加入到场景中)
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("登录界面");
primaryStage.setScene(scene);
primaryStage.show(); //显示
L2.setOnAction(e->{
L2.setCursor(Cursor.HAND);
ZhuCe open=new ZhuCe();
try
{
//Stage stage = new Stage();
open.start(new Stage());
primaryStage.hide(); //隐藏前一个界面
}
catch (Exception e1)
{
e1.printStackTrace();
}
});
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
关注公众号,获取免费软件、资料,笔记等。